Tag Info

Hot answers tagged

24

You could use named pipes (http://linux.die.net/man/1/mkfifo) on the command line of tee and have the commands reading on the named pipes. mkfifo /tmp/data0 /tmp/data1 /tmp/data2 cmd0 < /tmp/data0 & cmd1 < /tmp/data1 & cmd2 < /tmp/data2 & command -option1 -option2 argument | tee /tmp/data0 /tmp/data1 /tmp/data2 When command finishes, ...


23

You should understand that bash is just an execution environment. It executes commands that you call - it's not the business of the shell to even know what the command does, you can call any executable you want. In most cases, it's not even clear what an undo would do - for instance, can you "unplay" a movie? Can you "unsend" an e-mail? What would "undo ...


19

Commands run by at don't run in the terminal where they were registered. This wouldn't make sense in general: the terminal might not exist any more, or it might be in use by a different user. You may even have logged out by the time the command runs. Output from an at command is sent to you by email. That's local Unix email, not whatever external POP or ...


16

You could use gnuplot for this: primes 1 100 |gnuplot -p -e 'plot "/dev/stdin"' produces something like You can configure the appearance of the graph to your heart's delight, output in various image formats, etc.


14

If I understand correctly, you're looking for the equivalent of tee file1 file2 file3, but rather than write the same data to three files file1, file2 and file3, you want to pipe the same data into three commands cmd1, cmd2 and cmd3, i.e. … | ??? cmd1 cmd2 cmd3 should be equivalent to … | cmd1 & … | cmd2 & … | cmd3 & except that … would ...


11

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 this is file: hello world how are you today Then this would extract world from file: dd bs=1 skip=6 count=5 if=file It skips 6 bytes (hello_), reads 5 bytes (world), and ignores the rest. So with skip, count, and bs=1 (or alternatively, when using GNU dd, using the count_bytes/skip_bytes flag), you can extract specific byte ranges at byte ...


8

You need to quote the variables and avoid the command substitution: for i in ./*.mkv; do ffmpeg -i "${i}" -vcodec copy -acodec copy "${i}.mp4"; done See When is double-quoting necessary? for a detailed explanation of quoting. While I'm at it, the above produces files with a .mkv.mp4 extension; to fix that: for i in ./*.mkv; do ffmpeg -i "${i}" -vcodec ...


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 ...


7

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 ...


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" ...


7

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 ...


6

The difference is that cp is far clearer to humans. That is one of the first things you should be optimizing for. Using less in this way is so obscure, it's not really obvious that it works unless you try it out. The other answer points out that it doesn't work - if your file contains certain characters, and you expect the command to work without user ...


6

Yes it is possible, many ways, but some of the ways are less safe than others. There are two methods I recommend for this. The first and easiest way is to use sudo with the nopassword option, check the sudo manpage for details. The second (and more secure) way is to use pam (pluggable authentication modules) with a physical token. This is more complicated, ...


6

I would do this in R. You'll have to install it but it shouold be available in your distributions repositories. For Debian-based systems, run sudo apt-get install r-base That should also bring in r-base-core but if it doesn't, run sudo apt-get install r-base-core as well. Once you have R installed, you could write a simple R script for this: ...


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.


4

At least in bash you can skip mkfifo using process substitution: command -option1 -option2 argument | tee >(cmd1) >(cmd2) >(cmd3) or to adopt Arcege's example tee >(wc -l) >(wc -w) >(wc -c) < /etc/passwd >/dev/null


4

xclip uses the clipboard if you specify -selection clipboard: xclip -selection clipboard -o prints the contents of the clipboard (as filled with Ctrl-C in a GUI application). The equivalent option for xsel is -b (or --clipboard).


4

The reason why cannot interrupt that with Ctrl-c etc... is that the shell isn't running any command at that point. It's busy expanding {1..999999} to compute what the command line arguments will eventually be once it gets to the point of running the command. While external commands respond to termination signals like SIGINT (which is emitted by default when ...


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, ...


3

If myExe is in ./ and it defaults to writing to 4 files in ./ but you instead want those 4 files in /myfolder then you can do: (cd /myfolder && "$OLDPWD/myExe" params) ...which will still write those 4 files to ./ but change the value of ./ to /myfolder for only the length of time it takes to for ./myExe to write them.


3

less has been designed as a pager (e.g .with next page functionlity) for text files. As such it is less optimized for copying files than say cp (which has been designed to copy data), and might have a considerable overhead, thus lacking in performance. For starters, cp won't ask you for confirmation when handling files that contain special (e.g. control) ...



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