Bash tips

March 13, 2019
1 minute read
0 claps
Jump to Section

This post is as much for myself as it is for anyone else and I will update it whenever I come across anything that I find that is worth including. The below are all of the cool little bash commands for linux/unix that I have stumbled across.

manFollow this with any command for full instructions on how to use. Most commands can be followed with --help or -h as well for a reminder of the basics.
set -eSets option to exit a script if a command returns a non-zero exit code.
set -xOutputs the commands that get run as they run.
grepSearches an input and prints out lines that match a pattern.
<()Treats output as a file such as diff <(grep something) <(grep nothing).
!!Repeats the last command.
!$Repeats the last argument of the last command.
pushdChanges directory like cd but adds to stack so you can then go back with popd.
cd ~Change to home directory, cd on its own will also do this.
cd -Go back to previous directory.
!:1-$Takes all the arguments to the previous command and drops them in. !(last command) :1(first word) -$(to last word).
:hPut it after a filename, and it will change that filename to remove everything up to the folder.
exit 0Exit script with exit code zero.
headPrint first 5 lines of file.txt.
tailPrint last 5 lines of file.txt.
lsofLists open files. Use with -i :80 to find process listening on a port (port 80 here).
netstatLists network processes. If used with -plt switch it shows port numbers with programme name and PID. Useful if there is a process already listening on a port which you want to use.
ctrl-rReverse history search. Ctrl-r next item, ctrl-j to copy or enter to run.
topLive feed of processes using memory and how much memory they are using.
htopLike top but with a graphical interface.
teeSplits the output of a program so that it can be both displayed on screen and saved in a file.


Wrap Text
$ variable=$(git describe --tags)
$ echo "version is $variable"
version is 0.15.0

Here Docs

Input stream which sends the text in each line to a process until a line is reached which contains only the delimiter (ENDOFSTRING in the example below).

Wrap Text
This is a here doc
It will write every line to the file
until a line with only ENDOFSTRING
This is a here doc
It will write every line to the file
until a line with on ENDOFSTRING


Transforms or deletes characters in a string.

Wrap Text
$ echo "hello there" | tr a-z A-Z
$ echo "edward woodward" | tr -d "d"
ewar woowar

As mentioned above, I will keep updating this one. I expect I'll update the date each time to keep bumping it up the list. I'm also planning on starting a similar reference list of Vim commands :)


Concatenates files. In addition to writing a file to stdout, cat can also write to a file. Use cat with redirect ( > ) to the file you want to write to, write your message then ctrl-d to exit:

Wrap Text
$cat > file1.txt
Hello from file 2
$cat > file2.txt
Hello from file 2
cat file1.txt file2.txt > file3.txt
cat file3.txt
Hello from file 2
Hello from file 2

Creates a hard link to a file in another location. Use the -s switch to make it a soft link (also called a symbolic link or symlink for short) to a file in another location:

Wrap Text
ln -s target_path link_path

The files that you see on your filesystem are just pointers to something called an inode which is where the data is actually stored internally. A hard link is another pointer which points to the same inode. A soft link creates a new inode which references the original inode. This is an important distinction to make because deleting a hard link will delete the actual inode whereas deleting a soft link (symlink) does not affect the original file.


Compresses a directory or files(s).

Wrap Text
tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

The c switch signifies we want to create an archive, z says we want to use gzip to compress the file(s), v is verbose and f indicates that the following text will be the name of the filename we want to create. To unzip a tarball it is the same syntax as above except the c (create) switch is replaced with x for extract:

Wrap Text
tar -xzvf archive.tar.gz


Securely connect to a remote host.

Wrap Text
ssh your_username@remote_host_domain_or_ip_address


Secure copy files from remote host to local host or vice-versa. It uses SSH with the same level of security to transfer the files.

Wrap Text
scp foobar.txt your_username@remotehost:/some/remote/directory

Reverse the order of the arguments to copy in the other direction.


Recursively searches directories to find files.

Wrap Text
find /start/dir -name "something.*"

If you've found this helpful then let me know with a clap or two!