As programmers, we spend most of our time writing code to build software. However, when shifting that code to production, our coding skills may not be sufficient. In such cases, we rely on commands to accomplish various tasks, such as connecting to remote servers using SSH, checking file permissions, and editing/updating files on the server.
In this blog post, we will explore some of the most commonly used commands in Linux-based systems that will make your deployment process a bit easier (although not necessarily a piece of cake 😜!).
Since this article is a bit longer, I have divided it into the following sections.
- File commands 
- Search Commands 
- System commands 
- User commands 
- Permission commands 
- Process commands 
- Network and storage commands 
- Compression commands 
- SSH and File transfer commands 
File commands
As the word implies, this is everything to do with files and directories such as listing directories, printing the working folder, changing the folder, etc.
- pwdStands for Print Working Directory. Prints the path of the current working directory starting from the root
- lsDisplays the files and directories of the current working directory.
- ls DocumentsDisplays the contents of the Documents directory
- ls /Displays the contents of the root directory
- ls ..Displays the contents of the parent directory(one level above)
- ls ../..Displays the contents of the parent directory(two levels above)
- ls ~Displays the contents of the user’s home directory (/home/user)
- ls -d */Displays all the folders of the current working directory
- ls *Displays the contents along with the contents of subdirectories
- ls -RDisplays the files and directories in the current working directory, including their corresponding sub-directories down to the last file.
- ls -sDisplays the content with the size.
- ls -lDisplays the detailed content information, including file and directory permissions, the number of links pointing to the content, the user and group associated with the content, as well as the last modified date and time.
- ls -lhSimilar to the above command, It represents the size of files and directories in a readable format, such as 100B for bytes or 4.0K for kilobytes.
- ls -aDisplays the contents including hidden files
- ls -l -aor- ls -a -lor- ls -laor- ls -alDisplays the contents including hidden information
- ls -tDisplays the contents sorted by last modified date in descending order.
- ls -trDisplays the contents sorted by last modified date in ascending order
- ls -SDisplays the contents sorted by size in descending order
- ls -SrDisplays the contents sorted by size in ascending order
- ls > output.txtDisplays contents and directories and writes to- output.txtfile (Note: You can use any of the above-discussed flags and the ls command. such as- -s,- -R- -tetc.)
- mkdir testCreates a test directory in the current folder
- cd testChanges the current working directory to test
- cd ..Move up to one directory
- cd /Takes to the root directory.
- cdTakes to the home directory
- cd ~Takes to the home directory
- clearClears the terminal
- touch file1.txtCreates a- file1.txtfile in the current working directory
- nano file1.txtOpens the- file1.txtfile in the nano editor
- cat file1.txtShows the contents of- file1.txt
- less file1.txtShows less content of- file1.txt(Hit q to exit)
- mv file1.txt file2.txtRenames- file1.txtto- file2.txt
- cp file1.txt file2.txtCopy the contents of- file1.txtto- file2.txt
- cp file1.txt ~/dir2/file2.txtCopy a file to a different path
- rm file1.txtDeletes a file
- rmdir dir2Deletes a directory
- rm -R dir2Deletes a directory with files
Search Commands
In this section, you will explore the commands that would help you to search for specific files, directories, or content within files
- grep "five" file1.txtSearch the text five in- file1.txt
- grep "five" blog/posts/file1.txtSearch the text five in- blog/posts/file1.txt
- grep -r "five" blogSearch in the blog directory and its child contents for text five
- find -name file1.txtSearch the file- file1.txtin the current working directory
- find blog/posts -name 'file1.txt'Finds the file- file1.txtin the- blog/postsdirectory
- find blog/posts -name 'file*'Finds the files prefix with ‘file’ in the- blog/postsdirectory
- find -size +100MFinds the files which have more than 100 MB in size
System commands
Linux provides a wide range of system commands that allow users to interact with their operating system, gather information, and perform various administrative tasks. These commands play a crucial role in managing and maintaining a Linux system effectively.
- uname -aDisplays Linux system information
- uname -rDisplays kernel release information
- uptimeDisplays how long the system has been running and the load average.
- uptime -pSame as above but displays the output in a pretty format
- uptime -sDisplays how long the system has been up
- hostnameDisplays the system hostname
- hostname -IDisplay all local IP addresses of the host
- last rebootDisplays the system reboot history
- dateDisplays the current date
- calDisplays the calendar
- wDisplays the users who are online
- whoamiDisplays the logged-in user
- historyShows the audit of the user
- which curlShows the curl application path(installation location)
User commands
User management is a crucial aspect of Linux system administration. Linux provides a variety of commands to interact with user accounts, gather user-related information, and manage user privileges. These commands enable system administrators to effectively manage user accounts and ensure proper access control.
In this section, we will delve into some essential user commands that can assist you in user management tasks.
- cat /etc/passwdDisplays the list of users.
- getent passwdDisplays the list of users.
- The - cat /etc/passwdcommand and- getent passwdcommand displays the list of users in the system. The output is typically presented in the following format
redis:x:112:119::/var/lib/redis:/usr/sbin/nologin
The above line consists of seven fields, which are as follows:
- redis: User name.
- x: Encrypted password (the letter "x" indicates that the actual password is stored in the- /etc/shadowfile).
- 112: User ID number (UID).
- 119: User's group ID number (GID).
- /var/lib/redis: Full name of the user (GECOS).
- /usr/sbin/nologin: User home directory.
These commands provide important information about users on the system, such as their usernames, user IDs, group IDs, and home directories.
Ok. As you saw the cat /etc/passwd and getent passwd commands will display the output with seven fields.
However, you can also extract the part of the output by giving specific commands.
- awk -F: '{print $1, $2}' /etc/passwdDisplays the first two fields out of 7 fields
- cut -d: -f1,2,3 /etc/passwdDisplays the first 3 fields out of 7 fields
- sudo useradd test_user1Creates a new user called test_user1
- sudo passwd test_user1Sets the new password for user test_user1
- sudo useradd -m test_user2Creates a new user called test_user2 and also creates a test_user2 directory under the home directory.
- sudo useradd -m -d /opt/test_user3 test_user3Creates a test_user3 user and also creates a test_user3 directory under opt directory.
- sudo useradd -u 1600 test_user4Creates a test_user4 user with 1600 id
- id -u test_user4Returns the id of the user
- sudo useradd -g users login_user1Creates a new user and sets the login group to users
- id -gn login_user1Displays the group name of the user
- sudo useradd -g users -G user1,user2 login_user2Creates a new user called login_user2 and sets the primary group as users and secondary groups as user1, and user2.
- id login_user2Displays the userid, primary, and secondary group ids
- sudo groupadd managersCreates a new group called managers
- sudo chgrp managers file1.txtChanges the group to managers on- file1.txt
- sudo usermod -aG managers login_user2Adds login_user2 to the managers group
Permission commands
Understanding Permissions in Linux In Linux, permissions play a crucial role in controlling access to files and directories. They define what actions users can perform on specific resources, such as reading, writing, or executing files
For example, run the ls -l command in any of the directories which have some content.
drwxr-xr-x 1 777 shiva 4096 May 25 16:52 blog<br>-rw-r--r-- 1 shiva shiva 158 Jan 17 11:54 dump.rdb<br>drwxr-xr-x 1 shiva shiva 4096 May 25 12:52 shell-scripting
You will see similar output in your terminal. Let’s consider the first 10 characters (drwxr-xr-x) in the first line.
- The first letter denotes the content type such as directory or file. - drefers to the directory- -refers to the file.
- The second three letters refer to the user permissions 
- The third three letters refer to the group permissions 
- The last three letters refer to the other's permissions 
r read, w write, x execute, - no permission
u user, g group, o other people from the outside world, anall
+ adds permissions, - removes permissions, = adds new permissions and overrides existing
- chmod o+w file1.txtgives write access to the outside people on file1.txt
- chmod o-w file1.txtremoves write access for the outside people on file1.txt
- chmod g+wr file1.txtgives read-and-write access to the groups
- chmod u=rwx file1.txtgives read, write, and execute access to the user and overrides the existing permissions
Absolute Numeric Mode
| Number | Permission Type | Symbol | 
| 0 | No permission | 
 | 
| 1 | Execute | 
 | 
| 2 | Write | 
 | 
| 3 | Execute + Write | 
 | 
| 4 | Read | 
 | 
| 5 | Read + Execute | 
 | 
| 6 | Read + Write | 
 | 
| 7 | Read + Write + Execute | 
 | 
- chmod 111 file1.txtgives execute access to the user, group, and others
- chmod 750 file1.txtgives read+write+execute access to users and read+execute access to groups and no permissions to outside people.
- chmod -R 777 postsgive read+write+execute access to users, groups, and others on the directory posts and their contents.
Process commands
Processes are an integral part of any operating system, including Linux. They represent running programs or tasks that consume system resources. Linux provides a range of commands to manage processes efficiently, allowing users to monitor, control, and terminate them as needed.
- psdisplays currently running user processes
- ps -efdisplays currently running system processes
- ps -ef grep 12693displays the process information of 12693
- topshows the processes running(hit q to exit)
- htopalternative for the top (interactive process viewer)
- kill 24567kills the process 24567
Network and storage commands
Linux provides a variety of commands that enable users to interact with the network and storage components of their system. These commands allow you to gather network information, perform network diagnostics, manage storage devices, and retrieve files from the web. Understanding these commands can greatly enhance your ability to work with network and storage resources effectively.
- ifconfigshows the network information
- iwconfigshows wireless information
- ping- google.comreturns the continuous response from the host
- blkidshows the storage information
- dfshows the available and unavailable disk space
- lsusbshows the connected devices list
- lspcishows PCI information
- wget- https://www.sample-videos.com/img/Sample-jpg-image-50kb.jpgdownloads image file
Compression commands
Compression commands are indispensable when it comes to managing large sets of files or reducing file sizes for storage or transfer. Linux provides a wide range of compression commands that allow you to package files into archives, extract their contents, and compress them using various algorithms. Following are some of the example compression commands.
- tar cf testDir.tar testDircompress the- testDirand creates- testDir.tarfile
- tar xf testDir.tarextract the contents of testDir.tar
- tar czf testDir.tar.gz testDircreates gzip compressed tar file
- tar xzf testDir.tar.gzextract the compressed gzip file
- tar -tvf testDir.tardisplays the contents of the tar file
- tar -tvf testDir.tar.gzdisplays the contents of gzip tar file
- tar -xf nodejs.tar.xsExtracts the contents of nodejs.tar.xs
SSH and File transfer commands
Secure Shell (SSH) and file transfer commands are essential tools for remote access and transferring files between local and remote systems in Linux. SSH provides a secure encrypted connection, while file transfer commands enable efficient and secure file transfer over SSH. Following are some of the example SSH commands.
- ssh- root@domain.comconnect to the domain.com server with the root user (prompts for password)
- scp file1.txt- user1@domain.com- /tmpcopies the file securely to the- tmpfolder in the domain.com server
- sudo scp -r- user1@domain.com:/var/www /tempcopies files recursively from the server to the local machine
Ok. I know the list is too big. But, it is worth going through it. If you have these commands in your arsenal, you can definitely win the command line war.
 

 
    
Top comments (0)