Shell Commands

What is Unix Shell?

Unix shell is a command-line interface (shell) for Unix-like operating systems through which user enters commands as text. The command-line interpreter then executes the command and returns the results. The most common shells in Unix were Bourne shell and C shell, and then Bash (Bourne-Again Shell) was developed as the original Unix shell.

We will talk about most useful Bash commands but here are some very good free resources for you to learn more about shell and command line:

And here are some great books:

Unix File System

File system is an essential part of every operating system, and Unix is no exception. Unix file system is a tree like structure that supports three kinds of files: regular files, directories and special files that are files like Unix sockets and symbolic links. Everything in Unix is considered a file if is not a process. Even physical drives are considered as files, which should be mounted by the operating system for read/write access. The root of this structure is ‘/’, and each file under the root is identified by its path, name and a unique identifier (inode).

Unix support file extensions (e.g. .txt or .png) and also hidden files as other operating systems. The difference is that names of the hidden files start with ‘.’ and most of the times is used for configurations. For example, if you use git and you add global configurations, it will create a file named ‘.gitconfig’ in your home directory, which is hidden and is used by git to store the user’s configurations.

Shell Commands and Streams

Shell commands in Unix-like operating systems are executable through a command-line interface. They accept arguments and options, and the most obvious difference between them is that options start with ‘-‘. For example, many commands has a help option which gives information about the command and its arguments and options, and is specified by ‘-h’ or ‘–help’.

There are three kinds of streams that Bash commands work with: STDIN, STDOUT and STDERR. STDIN stands for ‘standard input’ and is the stream that a command accept to get input. It can be binary, text or even keyboard input. A command can write its outputs to STDOUT (standard output) which is the default stream that is printed out to the screen or STDERR (standard error), the error stream which is also printed to the screen by default.

Most Useful Commands

  • List files: ls
ls         #lists files in the current directory (same as 'ls .')
ls -l      #lists files in the current directory with details
ls -la     #lists all the files (even hidden files) in the current directory
ls --help  #prints help information about ls command
  • Change directory: cd
cd ~       #go to home directory (same as 'cd' without arguments)  
cd ../     #go to the parent directory
  • Copy files: cp
cp source.txt dest.txt   
cp -r mydir ~/               #copy the directory 'mydir' recursively to my home directory  
cd -i source.txt dest.txt    #prompt before overwritting
  • Move files: mv
mv source.txt dest.txt
mv mydir ~/                  #move the directory 'mydir' recursively to my home directory  
mv -i source.txt dest.txt    #prompt before overwritting
  • Remove files: rm
rm source.txt   
rm -r mydir       #remove directory 'mydir' recursively
rm -i *.txt       #remove all files with .txt extension but ask before removing 
  • Symbolic link: ln
ln -s /directory/foo.txt                  #make a symbolic link with the same name in the current directory
ln -s /directory/foo.txt myfile.txt       #make a symbolic link with another name (myfile.txt)
  • Current directory: pwd
pwd     #prints the current working directory
  • Create a directory: mkdir
mkdir mydir   #create a directory named "mydir" in the current working directory
  • Manual: man
man ls        #show manual page for ls command
  • Run a command as the root: sudo
sudo rm -rf /tmp/foo.txt        #execute the rm command as the root user