The Command Line Interface (CLI) is a tool for interacting with your operating system.This is a guide to some of the most commonly used CLI commands.
Navigating File System
pwd
- Print the working directoryUsage:
$ pwd home/user
ls
- List all files in the current directoryUsage:
$ ls # ls command used in home directory. Desktop Documents Downloads
Flags:
-a
- List all files including hidden files-S
- Sort files in size order-d
- List all directories-l
- List all files with detailed information
These are some important flags which used with the ls command. You also learn in detail by visiting this documentation.
cd
- Change directoryUsage:
$ cd directory_name
cd is used to move forward to the child directory.
$ cd ..
cd .. is used to move backward to the parent directory.
$ cd ~
cd ~ is used to move to the home directory.
Relative paths
$ cd ./directory_name/subdirectory_name
You can also use relative paths to move to a certain folder.
Absolute paths
$ cd ~/directory_name/subdirectory_name
A sample command using absolute path.
Managing Files and Directories
touch
- Create a new fileUsage:
$ touch file_name
This is the syntax to create a file.
mkdir
- Create a new directoryUsage:
$ mkdir directory_name
This is the syntax to create a directory.
Flags:
-p
- Create parent directories if they do not exist$ mkdir -p directory_name/subdirectory_name
This is the most commonly used flag . You can learn more about it here.
'rmdir' - Remove a directory
Usage:
$ rmdir directory_name
This is the syntax to remove a directory.
If you want to remove a directory which contains files, you can use this command.
$ rm -r directory_name
'rm' - Remove a file
Usage:
$ rm file_name
This is the syntax to remove a file. You can learn more about it here.
'cp' - Copy a file
Usage:
$ cp file_name destination
This is the syntax to copy a file.
'mv' - Move a file
Usage:
$ mv file_name destination
This is the syntax to move a file.To rename a file you can also use this command.
Viewing and Editing files
cat
- View a fileUsage:
$ cat file_name
This is the syntax to view a file.
$ cat > file_name #Create a new file $ cat file_name1 >> file_name2 #Append the contents of file_name1 to file_name2 $ cat -n file_name #View a file with line numbers
These are some stuffs used with cat commands. You can learn more about it here.
vi
,nano
- Edit a fileUsage:
$ vi file_name $ nano file_name
There are lot more commands for editing files. You can learn more about it here.
head
- View the first 10 lines of a fileUsage:
$ head file_name #View the first 10 lines of a file $ head -n 10 file_name #You can also specify the number of lines you want to view.
tail
- View the last 10 lines of a fileUsage:
$ tail file_name #View the last 10 lines of a file $ tail -n 10 file_name #You can also specify the number of lines you want to view.
less
,more
- View a file in a detailed mannerUsage:
$ less file_name $ more file_name
- Differences
more
: forward navigation and limited backward navigation.less
: both forward and backward navigation and also has search options. You can go to the beginning and the end of a file instantly.
- Differences
Some Useful Commands
man
- View a manual pageUsage:
$ man command_name #You view the manual page for every commands #Example usage $ man ls
wc
- Count the number of lines, words, and characters in a fileUsage:
$ wc file_name
- Flags (Frequently used):
-c
-> Count the number of characters-l
-> Count the number of lines-w
-> Count the number of words
You can learn more about it here
history
- View the command historyUsage:
$ history
Network commands
ping
- Ping an IP addressUsage:
$ ping google.com # Checks the network connectivity to specific server.
ifconfig
- View wired network informationUsage:
$ ifconfig # Displays the ip address and more stuffs related to network connection.
iwconfig
- View wireless network informationUsage:
$ iwconfig
ssh
- Securely connect to a remote serverUsage:
$ ssh [username]@[hostname or IP address]
netstat
- Network StatisticsUsage:
$ netstat
Flags:
-a
-> List all network connections-l
-> List all listening ports-at
-> List all network connections with TCP portsau
-> List all network connections with UDP ports
You can learn more about it here.
OS,Process Commands
ps
- List all processesUsage:
$ ps
- Flags (Commonly Used):
-ef
-> List all the process of the entire system with parent pid-aux
->List all the process with the CPU and memory usage
pstree
- List all processes in a tree formatUsage:
$ pstree
This image shows the senior most process with the sub process.
top
- View running processesUsage:
$ top #This command is used to view the running processes.
df
- View disk usage- Usage:
$ df -h #This command with -h is used ofter to display in human readable format.
- Usage:
uname
- Unix nameUsage:
$ uname #Displays information about the system.
Flags:
-a
-> Display all information
You can learn more about it here
free
- View memory usage- Usage:
$ free -h #Displays memory usage in human readable format.
- Usage:
lspci
- List all the PCI devicesUsage:
$ lspci #displaying information about PCI buses in the system and devices connected to them..
kill
- Kill a processUsage:
$ kill [pid]
Flags:
-9
-> Kill the process forcefully-15
-> Kill the process gracefully
You can learn more about it here
File permission
chmod
- Change file permissionUsage:
$ chmod [permission] file_name
- Arguments:
ugo
-> User, Group and Others$ chmod u+rwx g+rwx o+rwx file_name #The user,group,others have read,write and execute the file.You can modify the permission.
Binary Flags
- Read (4) - Write (2) - Execute (1) the values are represented like this.
$ chmod 755 file_name #The file will be readable,writeable and executable for user and readable, executable for group and other.
- You can learn more about here
chown
- Change file ownerUsage:
$ chown new_owner[:new_group] file_name
- Flags (Frequently used):
-R
-> Change the owner of the files and its subdirectories
- Flags (Frequently used):
sudo
- Run a command as root userUsage:
$ sudo command # Gives you the root user privileges. #Example usage $ sudo apt-get update
Package manager
apt-get
- Install a packageUsage:
$ sudo apt-get install package_name #root user privileges are used to install a package.
Searching Finding and Manipulating data
grep
- Search and manipulating text patterns in a fileUsage:
$ grep [pattern] file_name
Flags (Frequently Used):
-i
-> Case insensitive-l
-> List the files that contain the pattern-o
-> Print only the matched pattern
You can learn more about it here
sort
- Sort a fileUsage:
$ sort file_name
- Flags (Frequently used):
-r
-> Reverse the order of the sort-u
-> Remove duplicate lines
- Flags (Frequently used):
find
- Find filesUsage:
$ find [path] [type]-name pattern #Example command . Use type if necessary. $ find . -name '*.txt'
Flags (Frequently used):
-name
-> Find files with the pattern
You can learn more about it here
Bash Related Commands
Pipe Operator
|
- Combines the list of commandsUsage:
#The first command output is passed to the second command. $ command1 | command2
Example usage:
$ cat filename | sort
printenv
- Print environment variablesUsage:
$ printenv #Print all the environment variables.
xargs
- Execute commands from a inputUsage:
$ xargs command
Example usage
$ pgrep firefox | xargs kill -9 $ find . -name "*.txt" | xargs rm #xargs get input from previous command and execute it.
sed
- Search replace filtering text in a file and much more stuffs
Usage:
$ sed 's/searchString/replaceString/' file_name # s-String $ sed '1,2p' file_name # Print the first two lines.
- It can do much more other stuffs .You can learn from here.
awk
- Search and manipulate text patterns in a fileUsage:
$ awk 'pattern' file_name
Arguments (Frequently used):
- NR -> Get the line number
- NF -> Get the number of fields
Example usage
$ awk '{print $0,$1}' file_name # Print the first and second columns.
It is similar to sed . There are lot more useful patterns.You can learn from here.
Conclusion
These are some useful commands for a Software Engineer who just started with linux.
I hope you like this article.