Hot answers tagged

569

As of GNU coreutils 7.5 released in August 2009, sort allows a -h parameter, which allows numeric suffixes of the kind produced by du -h: du -hs * | sort -h For Mac, you can use this(from comments): brew install coreutils du -hs * | gsort -h


521

This will return true if a variable is unset or set to the empty string (""). if [ -z "$VAR" ];


184

One of the tricks I follow is to put # in the beginning while using the rm command. root@localhost:~# #rm -rf / This prevents accidental execution of rm on the wrong file/directory. Once verified, remove # from the beginning. This trick works, because in Bash a word beginning with # causes that word and all remaining characters on that line to be ignored. ...


147

There are several differences. In my opinion, a few of the most important are: [ is a builtin in Bash and many other modern shells. The builtin [ is similar to test with the additional requirement of a closing ]. The builtins [ and test imitate the functionality /bin/[ and /bin/test along with their limitations so that scripts would be backwards ...


137

This: date +%s will return the number of seconds since the epoch. This: date +%s%N returns the seconds and current nanoseconds. So: date +%s%N | cut -b1-13 will give you the number of milliseconds since the epoch - current seconds plus the left three of the nanoseconds. and from MikeyB - echo $(($(date +%s%N)/1000000)) (dividing by 1000 only ...


131

In Bash, when you're not concerned with portability to shells that don't support it, you should always use the double-bracket syntax: Any of the following: if [[ -z $variable ]] if [[ -z "$variable" ]] if [[ ! $variable ]] if [[ ! "$variable" ]] In Bash, using double square brackets, the quotes aren't necessary. You can simplify the test for a variable ...


124

The best method is to use screen. Another method is to use nohup. Screen is a "virtual" terminal which you can run from a "real" terminal (actually all terminals today are "virtual" but that is another topic for another day). Screen will keep running even if your ssh session gets disconnected. Any process which you start in a screen session will keep ...


114

A trick I use sometimes is to use base64 to encode the commands, and pipe it to bash on the other site: MYCOMMAND=`base64 -w0 script.sh` ssh user@remotehost "echo $MYCOMMAND | base64 -d | sudo bash" This will encode the script, with any commas, backslashes, quotes and variables inside a safe string, and send it to the other server. (-w0 is required to ...


113

A variable in bash (and any POSIX-compatible shell) can be in one of three states: unset set to the empty string set to a non-empty string Most of the time you only need to know if a variable is set to a non-empty string, but occasionally it's important to distinguish between unset and set to the empty string. The following are examples of how you can ...


101

SSH! SSH is the god command--I think it's the most valuable over-all command to learn. The options can be pretty daunting, but it seems like I'm constantly learning to use new command-line options for SSH that I never thought would be necessary. I may have used them all at this point. The more you use it, the more you learn about it. You can use it to do ...


98

I don't think a command or shell builtin for this exists, as it's a trivial subset of what the Bourne shell for loop is designed for and implementing a command like this yourself is therefore quite simple. For starters you can use a dummy for loop: for i in `seq 10`; do command; done Or equivalently as per JimB's suggestion, using the Bash builtin for ...


88

There are a few ways to do this, but the one I find most useful is to use GNU Screen. After you ssh in, run screen. This will start another shell running within screen. Run your command, then do a Ctrl-a d. This will "disconnect" you from the screen session. At this point, you can log out or do anything else you'd like. When you want to re-connect to the ...


87

See the uuidgen program which is part of the e2fsprogs package. According to this, libuuid is now part of util-linux and the inclusion in e2fsprogs is being phased out. (On new Ubuntu systems, uuidgen is now in the uuid-runtime package.) To create a uuid and save it in a variable: uuid=$(uuidgen) On my Ubuntu system, the alpha characters are output as ...


85

This will put your text into your variable without needing to escape the quotes. It will also handle unbalanced quotes (apostrophes). Putting quotes around the sentinal (EOF) prevents the text from undergoing parameter expansion. The -d'' causes it to read multiple lines (ignore newlines). read is a Bash built-in so it doesn't require calling an external ...


84

mysqldump --opt <database> | gzip -c | ssh user@wherever 'cat > /tmp/yourfile.sql.gz' You can't use tar in a pipe like this, and you don't need it anyway, as you're only outputting a single file. tar is only useful if you have multiple files.


81

I have a little script that extracts archives, I found it somewhere on the net: extract () { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar x $1 ;; *.gz) gunzip $1 ;; *....


81

To check for the CVE-2014-6271 vulnerability env x='() { :;}; echo vulnerable' bash -c "echo this is a test" it should NOT echo back the word vulnerable. To check for the CVE-2014-7169 vulnerability (warning: if yours fails it will make or overwrite a file called /tmp/echo that you can delete after, and need to delete before testing again ) cd /tmp; ...


78

The ; just separates one command from another. The && says only run the following command if the previous was successful cd /home; ls -al This will cd /home and even if the cd command fails (/home doesn't exist, you don't have permission to traverse it, etc.), it will run ls -al. cd /home && ls -al This will only run the ls -al if the ...


73

bash -x script or set -x in the script. You can unset the option again with set +x. If you just want to do it for a few commands you can use a subshell: `(set -x; command1; command; ...;)


73

I like to use cd - to switch to the previous directory. Very useful!


71

Avoid using /etc/mtab because it may be inconsistent. Avoid piping mount because it needn't be that complicated. Simply: if grep -qs '/mnt/foo' /proc/mounts; then echo "It's mounted." else echo "It's not mounted." fi


69

I've recently discovered the pv command (pipe viewer) which is like cat but with transfer details. So instead of $ gzip -c access.log > access.log.gz You can use $ pv access.log | gzip > access.log.gz 611MB 0:00:11 [58.3MB/s] [=> ] 15% ETA 0:00:59 So instead of having no idea when your operation will finish, now you'll know! Courtesy ...


65

In bash, the disown keyword is perfectly suited to this. First, run your process in the background (either use &, or ^Z then type bg): $ wget --quiet http://server/some_big_file.zip & [1] 1156 By typing jobs you can see that the process is still owned by the shell: $ jobs [1]+ Running wget If you were to log out at this point, the background ...


63

Most likely your ls is aliased to ls --color=auto, which tells ls to only use colors when its output is a tty. If you do ls --color (which is morally equivalent to ls --color=always), that will force it to turn on colors. You could also change your alias to do that, but I wouldn't really call that a good idea. Better to make a different alias with --color....


63

du | sort -nr | cut -f2- | xargs du -hs


62

a && b if a returns zero exit code, then b is executed. a || b if a returns non-zero exit code, then b is executed. a ; b a is executed and then b is executed.


59

To add variety without adding external dependencies, on Linux you can do: UUID=$(cat /proc/sys/kernel/random/uuid) To propagate bad practices, on FreeBSD, under the linux compatibility layer (linuxulator?), UUID=$(cat /compat/linux/proc/sys/kernel/random/uuid) References: UUID on Wikipedia. FreeBSD Bug #186187 - [linprocfs] [patch] emulate /proc/sys/...


56

In Short: [ ] are bash Builtins [[ ]] are bash Keywords Keywords: Keywords are quite like builtins, but the main difference is that special parsing rules apply to them. For example, [ is a bash builtin, while [[ is a bash keyword. They are both used for testing stuff, but since [[ is a keyword rather than a builtin, it benefits from a few special ...


53

sudo !! Rerun the previous command as root. [The current top command on the site http://www.commandlinefu.com, a site themed along the lines of this question.]



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