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.
|Follow this with any command for full instructions on how to use. Most commands can be followed with |
|Sets option to exit a script if a command returns a non-zero exit code.|
|Outputs the commands that get run as they run.|
|Searches an input and prints out lines that match a pattern.|
|Treats output as a file such as |
|Repeats the last command.|
|Repeats the last argument of the last command.|
|Changes directory like |
|Change to home directory, |
|Go back to previous directory.|
|Takes all the arguments to the previous command and drops them in. |
|Put it after a filename, and it will change that filename to remove everything up to the folder.|
|Exit script with exit code zero.|
|Print first 5 lines of file.txt.|
|Print last 5 lines of file.txt.|
|Lists open files. Use with |
|Lists network processes. If used with |
|Reverse history search. |
|Live feed of processes using memory and how much memory they are using.|
|Like top but with a graphical interface.|
|Splits the output of a program so that it can be both displayed on screen and saved in a file.|
$ variable=$(git describe --tags)$ echo "version is $variable"version is 0.15.0
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).
$ cat << ENDOFSTRINGThis is a here docIt will write every line to the fileuntil a line with only ENDOFSTRINGENDOFSTRINGThis is a here docIt will write every line to the fileuntil a line with on ENDOFSTRING
Transforms or deletes characters in a string.
$ echo "hello there" | tr a-z A-ZHELLO THERE$ 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:
$cat > file1.txtHello from file 2$cat > file2.txtHello from file 2cat file1.txt file2.txt > file3.txtcat file3.txtHello from file 2Hello 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:
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).
tar -czvf name-of-archive.tar.gz /path/to/directory-or-file
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:
tar -xzvf archive.tar.gz
Securely connect to a remote host.
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.
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.
find /start/dir -name "something.*"
If you've found this helpful then let me know with a clap or two!