Tag Info

Hot answers tagged

28

Adding & spawns a background process. If you write a; b, it will run command a, wait for it to finish, then run command b, in sequence. If you write a & b, it will spawn a as a background process. It will not wait for it to finish, and it will start running b immediately. It will run both at once. You can see what it does by experimenting in the ...


15

You can use <M-.> (or <Esc>. if your Meta key is being used for something else), that is, Meta-dot (or <esc> dot), where Meta is usually the Alt key, to recall the last argument of the previous command. So, first you would type $ grep foo /usr/share/dict/american-english And then if you wanted to grep for something else, you would type $ ...


14

Circular I/O Loop Implemented with tail -f This implements a circular I/O loop: $ echo 1 >file $ tail -f file | while read n; do echo $((n+1)); sleep 1; done | tee -a file 2 3 4 5 6 7 [..snip...] This implements the circular input/output loop using the sine algorithm that you mentioned: $ echo 1 >file $ tail -f file | while read n; do echo ...


9

The command that you must use for you is: id and for any other user: id username


9

What echo does with character escapes is implementation defined. In many implementations of echo (including most modern ones), the string passed is not examined for escapes at all by default. With the echo provided by GNU bash (as a builtin), and some other echo variants, you can do something like the following: echo -en 'first line\nsecond line\nthird ...


9

On a GNU system: find / -type d -print0 | shuf -zn5 | xargs -r0n1 cp foo (now copying the file to things like /sys or /proc would not make sense or even be possible, you may want to add -xdev to only select directories on the file system mounted at /). You could make it compatible with both FreeBSD and GNU with: find / -type d -print0 | sort -zR | tr ...


9

How about using cut? If you'd like to print the 2nd pattern echo "$above_string" | cut -f2 -d "?" Second column onward echo "$above_string" | cut -f2- -d "?"


8

If you want to crawl on dirs and subdirs: find /home/place/to/crawl -type f -exec file --mime-type {} \; | awk '{if ($NF == "image/jpeg") print $0 }' What it does? Search all inodes with the type file Execute the command file, to get a jpeg header of the file like: image/jpeg awk Edit: Added @Franklin tip, to use file with -i to use the mime string ...


8

You can use a FIFO for this, created with mkfifo. Note however that its very easy to accidentally create a deadlock. Let me explain that—take your hypothetical "circular" example. You feed a command's output to its input. There are at least two ways this might deadlock: The command has an output buffer. It's partially filled, but hasn't been flushed ...


8

Here is what the bash documentation says: PS1 The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is ``\s-\v\$ ''. PS2 The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is ``> ''. PS3 The value ...


7

You can use Bash's parameter expansion: string="foo-bar-123" && printf "%s\n" "${string##*-}" 123 If you want to use another process, with Awk: echo "foo-bar-123" | awk -F- '{print $NF}' Or, if you prefer Sed: echo "foo-bar-123" | sed 's/.*-//' A lighter external process, as Glenn Jackman suggests is cut: cut -d- -f3 <<< "$string" ...


6

You can translate special characters (binary data) into ordinary characters that are safe to display by piping your tail command into cat -v: tail --follow=name my-rolling-file.log | cat -v The -v (verbose) option of cat (also known as --show-nonprinting) displays "nonprinting" characters using the ^ and M- notations: 0x00 is displayed as ^@ 0x01 is ...


5

You asked for using some syntax with the echo command: echo $'first line\nsecond line\nthirdline' > foo (But consider also the other answer you got.) The $'...' construct expands embedded ANSI escape sequences.


5

You will receive in destination_dir files with full path from / find /path/git_directory -type f -iname "*.py" \ -exec cp --parents -t /path/destination_dir {} + Other solution is rsync rsync -Rr --prune-empty-dirs \ --include="*.py" \ --include="**/" \ --exclude="*" \ /path/git_directory ...


4

Add at the end of your script: read junk See Bash Manual for more info.


4

If I undersood the question correctly you need files in myfiles which do not have symlinks in images: #!/bin/bash OIFS="$IFS" IFS=$'\n' files="$(find myfiles/ -type f -name '*.jpg' -or -name '*.cr2')" for f in $files; do list="$(find -L images/ -xtype l -samefile "$f")" if [[ "$list" == "" ]]; then echo "$f does not have symlink." fi ...


4

There exists several variants of a watch command, some that spawn a shell to interpret a command line made of the concatenation of the arguments passed to watch (with space characters in between). In those you can do: watch 'ls | shuf' same as: watch ls '|' shuf (those watch actually run: "/bin/sh", ["sh", "-c", "ls | shuf"] and are quite dangerous in ...


4

Using Awk to print the second and third records separated by newlines: awk -F"?" '{printf "%s\n%s\n", $2,$3}' Elvis August 16 Leonard Nimoy February 27 If you want to swap out the record, you can set it as a variable: awk -v record=2 -F"?" '{print $record}' Elvis August 16


4

echo $above_string | grep -oP "^([^?]*\?){2}\K[^?]*" Change 2 to the n - 1 value in order to obtain the nth string. This assumes that you want the nth string in that line. You have n - 1 strings with no ? ending with a literal '?' (\? since it's a special character in perl regex). Then with \K you state you are not interested in the previous contents, ...


4

The best solution for this is to use awk: $ ldd /usr/bin/ppdhtml | awk '/ => / { print $1 }' | head -n1 libcupsppdc.so.1 To do this using grep, you will need to use the lookahead and lookbehind features of PCRE: $ ldd /usr/bin/ppdhtml | grep -Po '(?<=\t).+(?= => )' | head -n1 libcupsppdc.so.1 The lookahead and lookbehind features affect that ...


4

If your terminal is still open, type env: it will display all your environment variables. If it's a fresh install or if you never made any change, the most important variables are PATH (this one always exist) and LD_LIBRARY_PATH (may not exists, I'm not used to osx). For instance: $ env ... PATH=/usr/bin:/bin:/home/user/bin:/sbin ... It's also a common ...


4

auto eth0 is interfaces(5) syntax. It's a line you would add to /etc/network/interfaces, not a command to be run in a shell. Once you correctly configure the interface in /etc/network/interfaces, you can run the ifup/ifdown commands to apply them.


4

Launching a program from the terminal is as easy as running the executable. For example: $ firefox & The '&' above is optional, and it puts the process in teh background, which lets you immediately run another program in the same terminal. You can only pre-determine the screen location of the program's window if the program accepts an argument to ...


4

Using line continuations like that adds spaces into your string: the sequence backslash-newline-whitespace will be replaced by a single space. Just using a variables will go a long way towards improved readability: url="reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallylongstring" { somecheck || somecomand ...


4

What about this, tail --follow=name my-rolling-file.log | strings The default for strings is that it will only output printable characters in lengths of 4 (or more), but you can change this with -n {number}.


3

In the first line, xargs still waits for the second argument or an end of the input. After pressing Ctrl-D xargs continues with the rest and you will see the 5th x as single argument. This example may explain the behavior: (echo "x x x x x"; sleep 5; echo "x") | xargs -n2 Output: x x x x x x # after 5 seconds After the 6th x in the second echo ...


3

These numbers disambiguate manual pages that have the same name. They represent the manual section that the page should be retrieved from. As an excerpt from Wikipedia states: The manual is generally split into eight numbered sections, organized as follows [...]


3

You will need to store the output in a variable to accomplish this. Here is an example: if output=$(cmda); then printf '%s' "$output" | cmdb fi


3

date is separate from your shell, so unless you instruct your shell to modify the environment date sees, your changes to LC_TIME won't have any effect. You can fix this in two ways; either by specifying a value for LC_TIME only for date: LC_TIME=zh_CN.UTF-8 date or by exporting LC_TIME so its new value is given to all subsequent processes started by the ...


3

ls itself won't show this information. You can pipe the output of the find to file -f -, as follows: $ find /usr/local/bin | file -f - /usr/local/bin: directory /usr/local/bin/apt: Python script, ASCII text executable /usr/local/bin/mint-md5sum: ASCII text /usr/local/bin/search: ...



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