30
votes
Accepted
mv: rename <oldname> to <newname>/<oldname>: Invalid argument
The filesystem on your NAS is case-insensitive¹, so OldHDD and oldhdd are the same file. But your operating system is case-sensitive, so it considers those different names. The mv command asks the ...
23
votes
What is the difference between locate txt vs locate *.txt?
locate txt locates all files (of any type including regular, symlink, directory, socket...) whose path contains txt¹, so includes /foo/xtxty/bar, /foo/bar.txt, /foo/txt.bar, etc.
locate *.txt is ...
22
votes
Accepted
What does a hyphen do next to the argument position in bash shell script? Like ${1-}
A hyphen in a parameter expansion allows a default value to be specified. So ${1-} means “the value of the first parameter if it is set, and the empty string otherwise”.
That doesn’t seem particularly ...
20
votes
Accepted
Is it now safe to parse the output of GNU ls?
Is it now safe to parse the output of GNU ls? (with --zero)
--zero does help, a lot, but it's still not safe the way it was used here. There are issues with both the output format of ls itself, and ...
19
votes
Accepted
GNU Coreutils `\time --version` and `/bin/true --version` work but `\true --version` does not?
In Bash time is a keyword (see type time) and \time is not interpreted as such. \time makes Bash run an external executable which in your case is GNU time.
But true is a builtin (see type true). The ...
18
votes
Accepted
Use the 'cp' command in a Bash script and exclude a specific directory
You should enable the extglob option, it isn't enabled by default in scripts:
#!/bin/bash
shopt -s extglob
cp -var test/!(test2) testbkp
Also verify that the script is indeed running under bash.
16
votes
Accepted
Why xargs does not process the last argument?
What went wrong
b appears in the output, so it was processed, just not the way you expected.
As a first step, ask bash to tell you what it sees: pass the -x option to enable its traces.
$ echo a b | ...
16
votes
For loop through servers with custom ports (for i in "user1@server1 -p 12345" "user2@server2 -p 54321" ...; do)
Since version 7.7, OpenSSH's ssh (along with its scp and sftp) will accept the destination in the form of a URI. You can specify the port number as part of the URI if you like, for example "ssh://...
15
votes
bg command not sending process to background
From your description, the bg command did work, otherwise ctrl-z and ctrl-c would still work.
Just because a process is in the background doesn't mean it can't still send you outoutput. If you don't ...
15
votes
Accepted
Does ssh run commands (rather than shell itself) in a login shell?
login shell can mean two different things in that context:
the shell that is defined as the login shell for the user in the account database.
For instance:
$ getent passwd stephane
stephane:*:1000:...
15
votes
Understanding "side effect", or multiple commands within one command?
The : is called the null command. You can find its documentation in man bash:
: [arguments]
No effect; the command does nothing beyond expanding arguments and performing any specified redirections....
15
votes
Accepted
Running GNU screen through SSH, the shell is not a login shell?
From the man pages of GNU screen:
-s program
sets the default shell to the program specified, instead of the value
in the environment variable $SHELL (or "/bin/sh" if not defined). This
can ...
15
votes
For loop through servers with custom ports (for i in "user1@server1 -p 12345" "user2@server2 -p 54321" ...; do)
The right way would be to store that on config, rather than specifying everything on every connection.
On ~/.ssh/config you could have:
Host server1.domain1.com # When connecting to this
User user1 #...
14
votes
Does ssh run commands (rather than shell itself) in a login shell?
No, SSH only invokes a login shell when logging you into a shell, not when running a command. This means that if you want your login initialization files to run (e.g. ~/.profile), you need to do this ...
14
votes
Accepted
How to referencing $@ without pass it in bash function?
You can't without A making its positional parameters available to B one way or another. Could be by passing them along:
A() {
B "$@"
}
B() {
[ "$#" -eq 0 ] || printf '<%s>...
13
votes
Accepted
Why don't we use #!command for the shebang line?
A path-less shebang assumes that the command in the shebang is in the current directory, in the general case. More generically, a non-absolute shebang is interpreted relative to the current directory ...
12
votes
Accepted
Why does `trap` passthough zero instead of the signal the process was killed with?
$? contains the exit status of the last command that was run and waited for. You'll find that in:
$ bash -c 'trap "echo \$?" INT; sleep 10; exit'
^C130
130 was reported because both sleep ...
12
votes
Accepted
How to exclude swapfile from rsync backup?
The filter file is particular about its whitespace. For example, after the leading - (dash, space) everything is a relevant character. This includes trailing space.
You can check the filter file for ...
11
votes
Accepted
What does command eval "$(/opt/homebrew/bin/brew shellenv)" actually do?
$() is command substitution. It executes the command inside the parentheses and returns the output of that command.
It's often used to get the output of a program into a variable. e.g.
$ month=$(...
11
votes
Accepted
What is difference between these two declarations of associative arrays in Bash?
declare -A is the only reason your first declaration is treated as an associative array. Your second declaration is handled as an indexed array. When manipulating indexed arrays, indices are treated ...
11
votes
What does >&- do in a unix / linux terminal?
Quoting the POSIX specification for the sh language:
2.7.6 Duplicating an Output File Descriptor
The redirection operator:
[n]>&word
shall duplicate one output file descriptor from another, or ...
11
votes
Accepted
What does mean `^(*.c|*.md)`
In Zsh, ^ is a glob operator available when EXTENDED_GLOB is set, matching anything except the following pattern.
The parentheses group a pattern; this is useful in particular with disjunctions (|) ...
11
votes
Open PDF from a command line and go back to the command line
You don't actually need to press enter, you can use the terminal directly. What happens is that evince can print out various messages to standard error, so those make your temrinal look like it isn't ...
10
votes
Is there a way to do namespaces like in C for bash?
Well, there is no namespaces in C, in C++ - yes, there are. But none in C.
And no, there are no namespaces in bash either. bash has three name spaces - for local variables, functions and environment ...
10
votes
Accepted
Why does tar's handling of stdout and - differ?
The difference in behaviour comes from tar: when writing, it applies a “blocking factor”, which by default uses 10240-byte records (that’s 2800 in hexadecimal). This happens even when compressing, ...
10
votes
Is it now safe to parse the output of GNU ls?
If you're going to depend on the output of GNU ls specifically, that means you're dependent on the GNU Coreutils package. That means you can instead use another Coreutils utility, namely stat. Stat ...
10
votes
mv a bunch of files, but ask for each file
You could use the -p (prompt) option of xargs or the -ok predicate of find:
find ./*.jpg -prune -ok mv {} dest/directory/ ';'
printf '%s\0' *.jpg | xargs -0pI@ mv -- @ dest/directory/
(here assuming ...
10
votes
Accepted
For loop through servers with custom ports (for i in "user1@server1 -p 12345" "user2@server2 -p 54321" ...; do)
For a for loop that can loop over more than one variable, switch to zsh. You're already using zsh syntax by not quoting your variables:
# zsh
for colour host port (
green [email protected] ...
9
votes
Accepted
Does POSIX guarantee that all its shell utilities will resolve symbolic links where a file is expected?
POSIX does not require that all the utilities it specifies resolve any symbolic link provided as an argument expecting a file name or path. It does however document in detail how symbolic links should ...
9
votes
How do I keep a dash shell script running despite any error?
That's because . is a special builtin. That is a POSIX requirement. Most other POSIX shells also do it, but in the case of bash and zsh only when they're in their respective posix/sh/ksh mode (...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
shell × 11994bash × 4109
shell-script × 2388
linux × 1430
scripting × 728
command-line × 685
zsh × 518
quoting × 498
awk × 479
terminal × 446
sed × 423
ssh × 381
files × 375
text-processing × 374
io-redirection × 371
find × 366
grep × 347
pipe × 330
wildcards × 308
environment-variables × 270
variable × 246
ubuntu × 213
posix × 190
process × 185
filenames × 172