Tag Info

Hot answers tagged

16

I always get burned when I try using .* for anything and long ago switched to using character classes: chown -R username.groupname .[A-Za-z]* is how I would have done this. Edit: someone pointed out that this doesn't get, for example dot files such as ._Library. The catch all character class to use would be chown -R username.groupname .[A-Za-z0-9_-]*


9

Using the extended globbing (shopt -s extglob), you can use .!(.|) i.e. dot not followed by dot or nothing.


9

You can actually do this entirely in Bash, without any external commands, using word splitting and parameter expansion. It's even fairly short: EMAIL_ADDRESS=this.is.a.very-long_email.id@domainname.com USER=${EMAIL_ADDRESS%@*} WORDS=( $(IFS=._- ; printf '%s ' $USER) ) echo "${WORDS[@]^}" I'll take this line-by-line: USER=${EMAIL_ADDRESS%@*} This sets ...


6

The character . is only excluded from wildcard matching when it's the first character of the file name and it would be matched by a wildcard. In the pattern .*, the * matches strings beginning with ., so .* includes .. (as well as ., with * matching the empty string). This is a straightforward consequence of the pattern matching rules, annoying though it may ...


6

Consider using find (-maxdepth is a non-POSIX extension, but it should be readily available on Linux): find . -maxdepth 1 -type d -name '.*' -exec chown -R user:group {} +


6

If the directory itself shares the same ownership as its files (hidden or not), then you can chown it recursively instead. The -R option will include hidden files when recursing inside the current directory. $ chown user:group . -R # Will include all hidden files


6

You know, of course, that $(…) causes the command(s) within the parentheses to run in a subshell.  And you know, of course, that jobs is a shell builtin.  Well, it looks like jobs clears a job from the shell’s memory once its death has been reported.  But, when you run $(jobs), the jobs command runs in a subshell, so it doesn’t get a chance to tell the ...


6

If you're using bash, you may have better luck declaring it as: function grom() { … } (Note: function will not work in strict POSIX shells like dash!) @aug suggested (via edits to this answer) that this is due to a conflicting alias (or, less plausibly, a builtin that somehow got defined). The reserved word function either alters the loading order to ...


6

With sed: sed 's/@.*//; s/[-_.]/ /g; s/\<./\U&/g' <<END user.name@domainname.com user.name2@domainname.com this.is.a.very.long.email.id@domainname.com END User Name User Name2 This Is A Very Long Email Id Requires GNU sed for the \U upper case directive. https://www.gnu.org/software/sed/manual/sed.html#The-_0022s_0022-Command Expanded: ...


5

[[ is a bashism. /bin/sh is not guaranted to be the Bourne Again shell. Even on Linux operating systems, it could be the Debian Almquist shell, or the Policy-Compliant Ordinary shell. On the BSDs, it is not the Bourne Again shell out of the box because on the BSDs the Bourne Again shell is an optional add-on to the operating system proper. It's in the ...


4

You are already re-logged in with the new ssh session that you set up. If you want to connect the old edit session to you newly logged in session you can try reptyr which "reparents a running program to a new terminal". If this disconnecting happens often there are multiple things you can do: set ServerAliveInterval and/or TCPKeepAlive in ...


4

The basic syntax you need is this: read date1 date2 < <( curl ... | gawk '...' ) This way you need just one awk instance as illustrated here (without the seconds conversion which you'd have to add; see below): read date1 date2 < <( curl ... | awk ' /Newest Sequence/ { new=$3" "$4 } /Oldest Sequence/ { old=$3" "$4 } END { ...


4

You can access the array index using ${!array[@]} and the length of the array using ${#array[@]} e.g. #!/bin/bash array=( item1 item2 item3 ) for index in ${!array[@]}; do echo $index/${#array[@]} done Note that since bash arrays are zero indexed, you will actually get 0/3 1/3 2/3 If you want the count to run from 1 you can replace $index by ...


3

Note that in when dealing with $@ you can simplify it to for a do echo "$a" done If `in WORDS ...;' is not present, then `in "$@"' is assumed.


3

Assuming you mean the byte offset, from man od -A, --address-radix=RADIX output format for file offsets. RADIX is one of [doxn], for Decimal, Octal, Hex or None so for example od -An file


3

From the bash documentation: Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows: \a alert (bell) (...) \nnn the eight-bit character whose value is the octal ...


3

First, let's look at what we see in the terminal: $ echo <(vim) /dev/fd/63 $ Vim: Warning: Output is not to a terminal Notice that you get a prompt back immediately, without waiting for the editor to terminate. Process substitution doesn't wait for the command to finish, it creates a pipe between the command and the shell. A name for that pipe is ...


3

The diagnostic "unexpected end of file" is a hint that you have some unmatched or unterminated opening syntactical construct (if w/o fi, do w/o done, opening brackets w/o the associated closing one, opening but unterminated quotes, etc.). The line number pointing to the end of the script is not helpful in this case, beyond saying to inspect your syntactical ...


3

You can check what locations are currently checked for direct commands by looking at the $PATH variable: echo $PATH It's likely this includes /usr/local/bin, in which case you could put a symbolic link there: ln -s /opt/mysuperscript /usr/local/bin/mysuperscript Now you can just type mysuperscript to run your script.


2

bash has both an interactive mode and a batch mode. It enters the corresponding mode depending if stdin is a terminal or not. bash # interactive cat|bash # non-interactive: stdin is a pipe not a terminal cat|bash -i # explicitly request interactive mode In interactive mode bash will print a prompt (configurable by the PS1 variable) and also set ...


2

The code "%$width.${width}s\n" generates a format string that is suitable for consumption by printf In the script that you posted, width has been assigned the value 55, so that both $width and ${width} are expanded by bash to 55: the entire first parameter to printf expands to %55.55s\n; this is the format %s, with a filed width and precision specifiers ...


2

The command for that is cp: cp file1.txt file2.txt If you want to append to another existing file you can use: cat file1.txt >> file2.txt


2

cp is usually used for copying. cp file1.txt file2.txt To append to an existing file, use cat file1.txt >> file2.txt


2

Actually, sed can also take ranges. This command will delete all lines between Match user foo and the first empty line (inclusive): $ sed '/Match user foo/,/^\s*$/{d}' file Match user bar ChrootDirectory /NAS/bar.co.uk/ ForceCommand internal-sftp AllowTcpForwarding no GatewayPorts no X11Forwarding no Match user ...


2

In bash ≥3.0 (and zsh, and ksh93), {1..40} will expand to the numbers from 1-40 (inclusive). In a POSIX shell like dash (which is typical of /bin/sh in e.g. Ubuntu), it will not work (we call this issue a "bashism"). On systems with the GNU utilities, you can use seq to accomplish this: for i in $(seq 1 40) do echo $i done To be more portable, ...


2

With the dig command, you do not have to perform any additional parsing of the output: $ dig stackexchange.com +short 198.252.206.140 When looking for an internal-only hostname, it might be wise to use the +search parameter: $ dig myinternalhost +search +short 192.168.1.120


2

If I understand correctly, you are saying that this command: java -jar $(find . -type f -name "myjar-*" -print | head -n 1) -ai Returns this output: Version: 2 Name: MyName Company: ABC And you want to check whether that output contains a line that starts with Name: and contains the string Na (excluding the Na in Name:). If so, you could do: ...


2

I'm working on Linux, which means the is the command md5sum which outputs: > md5sum * d41d8cd98f00b204e9800998ecf8427e file_1 d41d8cd98f00b204e9800998ecf8427e file_10 d41d8cd98f00b204e9800998ecf8427e file_2 d41d8cd98f00b204e9800998ecf8427e file_3 d41d8cd98f00b204e9800998ecf8427e file_4 d41d8cd98f00b204e9800998ecf8427e file_5 ...


2

The colours are set by ls, using the LS_COLORS environment variable. To change the colours, you can use dircolors. dircolors --print-database outputs the current source settings, which you can store in a file and adapt; then dircolors ${file} will output the processed LS_COLORS value for you using the settings in ${file}. Strictly speaking ls outputs ...


2

Strings in single quotes are used as-is. In a single-quoted string, the only special character is the ' single quote character, which ends the string. In strings in double quotes, the characters "\$are special:"ends the string,` makes the next character lose its special interpretation, ` starts a command substitution, and $ starts a variable substitution, ...



Only top voted, non community-wiki answers of a minimum length are eligible