Tag Info

Hot answers tagged


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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.


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


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


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


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.


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


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


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


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


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


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.


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


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.


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

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