Tag Info

Hot answers tagged

255

Use GNU coreutils >= 7.5: du -hs * | sort -h


233

This will return true if a variable is unset. if [ -z "$VAR" ];


154

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


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


86

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


85

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


85

The best method is to use screen. Screen is a virtual terminal which you can run from a "real" terminal (actually all terminals today are virtual). Screen will keep running even if your ssh session gets disconnected. Any programm which you start from screen will keep running in that screen session. When you reconnect the ssh session, you can reconnect to ...


82

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

A trick I use some times is to use base64 to encode the commands, and pipe it to bash on the other site: MYCOMMAND=`base64 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. On the other side, base64 ...


80

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


79

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


78

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


73

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


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


67

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.


63

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


56

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


55

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


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


49

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


49

Actual password hashes are stored in /etc/shadow, which is not readable by regular users. /etc/passwd holds other information about user ids and shells that must be readable by all users for the system to function.


46

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


46

Press Ctrl-R and start typing a command (or any part of it) - it searches the command history. Hitting Ctrl-R again will jump to the next match, enter executes the currently displayed command, and right-arrow (at least) will let you edit it first. $ (reverse-i-search)`svn': svn status I had used Linux for something like 7 years as my main OS before ...


46

Add space before command. commands starting with a space do not put in history: root@ubuntu-1010-server-01:~# echo foo foo root@ubuntu-1010-server-01:~# history 1 echo foo 2 history root@ubuntu-1010-server-01:~# echo bar bar root@ubuntu-1010-server-01:~# history 1 echo foo 2 history man bash HISTCONTROL A ...


46

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


46

Try one of these: stty sane or reset If both don't work, or your terminal is so messed up that you can't even enter commands, then it is best to close the terminal and start a new one. Note that stty sane is defined by POSIX whereas reset is not. That means on some systems there might not be a reset or it might do something completely different, like ...


45

The command line is a funny thing. I think that you can only learn so much on your own and the rest you learn by accident watching somebody else using a command line. I was using the shell for years painstakingly typing in directory names by hand. One day I was watching a friend mess around on a system and he kept hitting the tab key. I asked "why are ...


43

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


41

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



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