Tag Info

Hot answers tagged

47

just type: alias gb='cd /media/Dan/evolution' To make this setting permanent (so that it sticks after you restart or open another console) add this line to the file ~/.bashrc (assuming you use the bash as your default shell)


41

> is the default continuation prompt.That is what you will see if what you entered before had unbalanced quote marks. As an example, type a single quote on the command line followed by a few enter keys: $ ' > > > The continuation prompts will occur until you either (a) complete the command with a closing quote mark or (b) type ...


37

lynx has a "dump" mode, which you can use with watch: $ watch lynx https:/www.google.com -dump From man lynx: -dump dumps the formatted output of the default document or those specified on the command line to standard output. Unlike interactive mode, all documents are processed. This can be used in the ...


37

Set the shell's noclobber option: bash-3.2$ set -o noclobber bash-3.2$ echo hello >foo bash-3.2$ echo hello >foo bash: foo: cannot overwrite existing file bash-3.2$


36

Alternative to aliasing gb() { cd /media/Dan/evolution; } This defines shell function gb, which takes no arguments, and performs cd /media/Dan/evolution. As with other suggeststions, this can be added to ~/.bashrc


35

tac/grep Solution tac file | grep whatever Or a bit more effective: grep whatever < <(tac file) Time with a 500MB file: real 0m1.225s user 0m1.164s sys 0m0.516s sed/grep Solution: sed '1!G;h;$!d' | grep whatever Time with a 500MB file: Aborted after 10+ minutes. awk/grep Solution: awk '{x[NR]=$0}END{while (NR) print x[NR--]}' ...


34

If the first character of file name is printable but neither alphanumeric nor whitespace you can use [[:punct:]] glob operator: $ ls *.txt f1.txt f2.txt ♫abc.txt $ ls [[:punct:]]*.txt ♫abc.txt


33

You should use the at command: $ sudo at 6:45 [sudo] password for root: warning: commands will be executed using /bin/sh at> poweroff at> <EOT> Don't type the <EOT>, but press Ctrl+D at the second at> prompt. The significant advantage of using at over using shutdown with a TIME argument, is that it involves real, persistent, ...


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


26

Probably going to get my knuckles rapped for this, here's a hacky combination of bash brace expansion and eval that seems to do the trick eval {stat,file}" fileName;"


24

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


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 can use brace expansions: convert -trim -density 400 this_is_a_very_long_filename_of_my_pdf_file.{pdf,png}


22

sed Solution: sed -e 1b -e '$!d' file When reading from stdin if would look like this (for example ps -ef): ps -ef | sed -e 1b -e '$!d' UID PID PPID C STIME TTY TIME CMD root 1931 1837 0 20:05 pts/0 00:00:00 sed -e 1b -e $!d head & tail Solution: (head -n1 && tail -n1) <file When data is comming from a ...


22

With GNU, FreeBSD or OS/X date (or date implementations that use the system's libc's strftime() where that is the GNU libc), adding hyphen - after % prevents numeric fields from being padded with zeroes: $ date +'%Y%-m%d' 2015120 From man date on a GNU system: By default, date pads numeric fields with zeroes. The following optional flags may ...


21

With zsh, you can use anonymous functions: (){stat $1; file $1} filename With es lambdas: @{stat $1; file $1} filename You could also do: { stat -; file -;} < filename (doing the stat first as the file will update the access time). I'd do: f=filename; stat "$f"; file "$f" though, that's what variables are for.


21

du -sh is a good place to start. The options are (from man du): -s, --summarize display only a total for each argument -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) To check more than one directory and see the total, use du -sch: -c, --total produce a grand total


20

Using GNU grep for the colouring: color() { GREP_COLOR=$1 grep --color '.*'; } (tail -qf /var/log/syslog | color 31 & tail -qf /var/log/fail2ban.log | color 32 & tail -qf /var/log/nginx/error.log | color 33) Note that the first 2 are started in background. That means they won't be killed if you press Ctrl-C (shell explicitly ignore SIGINT for ...


19

In Bash, you can use Bash's built in string manipulation. In this case, you can do: > text="some text with spaces" > echo "${text// /}" sometextwithspaces For more on the string manipulation operators, see http://tldp.org/LDP/abs/html/string-manipulation.html However, your original strategy would also work, your syntax is just a bit off: > ...


19

You can use shutdown: sudo shutdown -h 06:45 & And to check it: ps -aux | grep shutdown If you want to cancel it: sudo shutdown -c This assumes of course that the shutdown time has already passed.


19

IFS stands for "internal field separator". It is used by the shell to determine how to do word splitting, i. e. how to recognize word boundaries. Try this in a shell like bash (other shells may handle this differently, for example zsh): mystring="foo:bar baz rab" for word in $mystring; do echo "Word: $word" done The default value for IFS consists of ...


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


17

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.


16

This solution might help: tac file_name | grep -e expression


16

That will happen if you have an unclosed quote in your command. That's something like: $ echo "test here > > ... You can exit that mode by closing the quote (write a " or ', or whatever your open quote is). It could also be a brace-delimited block, a partially-complete for-do or while-do loop, or certain other constructs. You can also press Ctrl-C ...


16

In Unix, most objects you can read and write - ordinary files, pipes, terminals, raw disk drives - are all made to resemble files. A program like cat reads from its standard input like this: n = read(0, buffer, 512); which asks for 512 bytes. n is the number of bytes actually read, or -1 if there's an error. If you did this repeatedly with an ordinary ...


15

Easy trick for alias in $(compgen -a); do type $alias; done


15

There are several options to do so: You can use a terminal multiplexer like screen or tmux. In screen, for example, the shortcut Ctrl+a - a, has the same functiononality as Alt+Tab in graphical environments: switch to the last screen. Or you use vim's internal function. Type :!command in vim's command mode. For example: :!ls -l. After the command ...


15

The command line arguments of every process in the system is considered "public". Not just the w command, but ps and top and many other commands access that information as a matter of course. Indeed no special privileges are required to get that information. On Linux, you can read the command line of another process, even a process belonging to another user, ...


15

Because builtins are part of the shell. Any bugs or history they have are bugs and history of the shell itself. They are not independent commands and don't exist outside the shell they are built in. The equivalent, for bash at least, is the help command. For example: $ help while while: while COMMANDS; do COMMANDS; done Execute commands as long as a ...



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