This is a “go through” guide intended to help newcomers and medium Linux users on consulting useful bash commands.
This is a living web page! Which means it will be updated on a regular basis as more and more people contribute to it :)
This is not a guide, nor a tutorial for Linux, Shell or Bash. For reals. It’s just a collection of some good commands, materials, tips and tricks gathered from all around the internet and presented in a nice way so you can easily find it.
Thank you and keep bashing!
Introduction: basic linux knowledge
- This is probably the most important lesson you will ever learn on Linux: man pages are your best friend! Learn to read them. Learn to understand them. Learn to love them.
To access a man page on any linux program just type:
$ man <program>
- To view a short information on any program:
$ whatis <program>
- You can also find out where it is located by typing:
$ whereis <program>
- To change user:
$ sudo -u <user>
- To change user to superuser (root):
$ sudo su
Attention: This is not recommended since as a superuser you have total control of your system, hence you can end up fucking it badly.
”/” represents the root directory.
”~” is an alias for your user’s home directory, which usually is: /home/youruser/.
”.” represents the current directory
To decompress a tarball:
tar -vzx <compressed-file.tar.gz>
Note: Flag -v is for verbosity.
- To see your session history:
Pretty useful stuff:
|is a piping operator. It is used for inserting the output of a program into the input of another program.
Listing all process called “init”:
ps -aux | grep init
- You can quickly write something into a file by doing:
echo "any random sentence" >> any-random-file.txt
The ‘»’ operator will concatenate the sentence to the end of the file.
BE CAREFUL not to use ‘>’ instead of ‘»’ since the former will OVERWRITE the content of the file instead of adding the sentence to it.
- You can call a previously used command pra typing “!” plus the initials of the said program.
For example, let us say you want to edit .bashrc and then open it to edit again:
uname -awill tell you information about your system
df -hwill tell you about your file system disk space
Working with files
- Where am I?
- Creating Files
$ > filename
- Changing directories
$ cd path/to/directory
- Moving things around
$ mv -v <filename> /another/path/
- Deleting things FOREVER
sudo mv -v <filename> /dev/null/
- Copying stuff
$ cp -v <filename> <another-filename>
- Deleting files
$ rm -v <filename>
- Deleting folders
$ rm -vR /path/to/<folder>
- Updating timestamp:
$ touch <filename>
- Listing files and directories
$ ls -al
-a tells ls to list all files.
- Files starting with
. are hidden files. (Remember this).
- Printing the content of a file:
- Printing the last 2 lines of a file:
tail -n 2 <filename.txt>
- Printing the first 2 lines of a file:
head -n 2 <filename.txt>
Working with environment and variables
Listing local variables
Listing global variables
Printing variable content
1 2 $ foo='This is a variable!' $ echo $foo
Looking for a local variable
$ set | grep foo
Exporting it to env
1 2 $ export foo $ env | grep foo
Useful environment variables
$PS1your prompt setup
$PATHyour path setup
$USERyour current user
The most important files regarding your environment are:
Both of them are shell scripts and contain instructions which are executed when you log in.
Permanently exporting variables to your PATH:
`$ echo ‘export $PATH=”$PATH:/path/to/file/”’ » ~/.bashrc
Managing and killing processes
To list all processes on your system
$ ps -aux
To list all processes running as root
$ ps -U root -u root u
To list all processes owned by you
$ ps x
Searching processes by keyword
$ ps -aux | grep '<keyword>'
Listing it Tree style
$ ps -aux --forest
Killing a specific process
$ sudo kill -9 <PID>
Killing all processes except for kill and init
$ sudo kill -9 -1
To change the owner of a directory:
$ sudo chown -R newowner:newowner
To view actual files permissions
$ ls -l
First letter is the file type:
d b c p s - directory block file special file pipe socket regular file
- Second, third and fourth letters are the user permissions
- Fifth, sixth and seventh letters are the group permissions
- Eighth, ninth and tenth letters are the others permissions
r w x - read write execute disable
To change the files permissions:
- Using letters:
u g o a user group others all
+ - = add permission remove permission changes permissions to the inserted
r w x read write execute
$ chmod a+w fileadd write permission for all users
- Using numbers
read write execute 4 2 1
$ chmod 754 fileset permission to file:
user group others 7 5 4 read + write + execute read + execute read
- Using letters:
Search, save and simplify command outputs:
Bit bucket (as known as the black hole, one of the linux’s special files):
Standard input stream:
Standard outpout streams:
Tailing files (getting the last lines)
$ tail <file>
Outputing whole file content
$ cat <file>
STDIN to a file (stop with
CTRL + C)
cat > <file>
Reading from file to another file
cat <file1> > <file2>
Reading all lines of file with line numbers
cat -n <file>
Saving output to file (using nano editor)
$ <command> | nano file_name
Searching through the output
$ <command> | grep <term>
Displaying output in a file-like style (
less allows searching by pressing
$ <command>|less or
Preventing terminal hanging (command outputs will still be shown in the terminal)
Ignoring file output completely (note that
2 = STDERR and
1 = STDOUT)
$ <command> > /dev/null 2 > &1
Customize your bash cursor
PS1 environment variable contains the style for the bash cursor:
Export it to your ~/.bashrc file:
export PS1='\u@\h \$'
This will print the following as a cursor:
Some formatting options can be:
\h - The hostname, up to the first ' . '
\H - The hostname.
\n - A newline.
\t - The time, in 24-hour HH:MM:SS format.
\T - The time, in 12-hour HH:MM:SS format.
\@ - The time, in 12-hour am/pm format.
\u - The username of the current user.
\w - The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable).
\W - The basename of $PWD, with $HOME abbreviated with a tilde.
Show all network interfaces
Configure a wireless network interface
Get more information about wireless interface
$ iwlist <your_interface_here> scan
Check hardware information include about your network, this shows PCIs drivers which is installed or not
Show who is connected in your network
$ nmap 192.168.0.*
Verify if you have any open port
$ nmap <your_ip>