Linux One Liners

I have collected few linux one liners for linux/unix shell script. The majority should work on bash, but some may work with other shells as well. Out of necessity in different situations, I have found some of the one liners. Others are collected from the net which I found interesting. Most of the one liners presented here are the simpler ones so that they are self-explanatory. For a comprehensive collection of linux one liners you can visit .

Go back to previous directory

Quickly go back to the previous working directory. The command is useful to go back and forth between two directories.

$ cd -

Copy public key over ssh

SSH is an essential tool for secure remote login and executing remote commands. Each time one tries to login user needs to type in the password for authentication. An alternative is to use public key authentication. The private-key public-key pair can be generated using ssh-keygen command on local machine. Finally, the public key needs to be stored in the remote machine. The following one liner does exactly this.

$ ssh remote-machine 'cat >> .ssh/authorized_keys' < .ssh/

Reuse arguments from last command

Sometimes we need to reuse the same argument we used in the last command such as a long path name. If the argument is particularly long, then instead of typing it again we can use a shortcut. There are few variations of such shortcuts. To reuse the last argument of the last command only type !$ in place of that argument. For example

$ cp !$ .

will copy the file which is the last argument of the last command in the current working directory. To reuse all arguments from last command use !* instead. For instance

$ ls !*

will list all files in all the paths which used as arguments in the last command. If we need to ignore the last argument but reuse the rest of the arguments we can use !-.

List all file types

Here is long one liner that lists all file types found under the current working directory. File type is assumed to be the last part of the file name after the dot(.) and if the file name does not contain dot(.) then its file type is not listed.

$ find . -type f | sed '/\/[^.]*$/d' | sed 's/^.*\.//' | sort | uniq

List total sizes of subdirectories

The following one liner is useful to quickly summarize the total sizes of all subdirectories under the current working directory.

$ du -hs ./*/

Repeat last command

There is a neat little shortcut to repeat from the command history.

$ !!

If the last command failed requiring super user privilege, you can just repeat it like this.

$ sudo !!

Correct spelling mistake in last command

What if you type a long command on the shell prompt, only to discover you made a spelling mistake somewhere in the command. Instead of typing the command again, to correct say “foo” to “bar” you can type

$ ^foo^bar^

This command however corrects only the first occurrence of “foo”. If you one to correct all occurrences of “foo” then you can use following command.

$ !!:gs/foo/bar

Count lines in a file

To count the number of lines in a file, the linux command wc (short for word count) comes in handy.

$ cat filename | wc -l

Copy whole directory

There are couple of ways to copy one directory to another location along with all the sub directories. For instance using the recursive option with cp command

$ cp -r sourcedir destdir

However, my personal favorite is to use tar command as it can be combined with find command. That way, only selected files can be copied keeping the directory structure intact.

$ tar -C sourcedir -cpf - . | tar -C destdir -xpf -

Batch rename files

For this one liner we will use the find command which comes in handy in many situations. Specially with the -exec option or xargs command and linux piping a myriad of tasks can be achieved just in a single line.

For instance, I frequently have to edit lots of jpeg photos in photoshop and I usually save them in the same folder. I usually name the edited photos like ‘some_picture_1.jpg’ to distinguish from the original photo. But, sometimes it happen that after editing I saved the photo by replacing the original one. Now, luckily I still have them in my camera, and I can download them again. Before doing that, I need to rename only the jpeg files that I have edited. I can do something like this.

$ find . -name '*.jpg' -mtime 0 -exec \
mv $1 `echo $1 | sed s/.jpg/_1.jpg/`' command '{}' \;

Now, agreeably its not a short one liner and its not pretty. But it covers few different tricks on its own. Let’s break it down and explain part by part. The

$ find . -name '*.jpg' -mtime 0

part finds all the jpeg files with ‘jpg’ extension in the current directory and all sub-directories which have been modified during last hour. Then for each of those files, we run a shell command which basically renames that file. The part within back quotes

$ `echo $1 | sed s/.jpg/_1.jpg/`

gives the new name for the file.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s