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.
pwd
Stands for Print Working Directory. Prints the path of the current working directory starting from the rootls
Displays the files and directories of the current working directory.ls Documents
Displays the contents of the Documents directoryls /
Displays the contents of the root directoryls ..
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 directoryls *
Displays the contents along with the contents of subdirectoriesls -R
Displays the files and directories in the current working directory, including their corresponding sub-directories down to the last file.ls -s
Displays the content with the size.ls -l
Displays 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 -lh
Similar 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 -a
Displays the contents including hidden filesls -l -a
orls -a -l
orls -la
orls -al
Displays the contents including hidden informationls -t
Displays the contents sorted by last modified date in descending order.ls -tr
Displays the contents sorted by last modified date in ascending orderls -S
Displays the contents sorted by size in descending orderls -Sr
Displays the contents sorted by size in ascending orderls > output.txt
Displays contents and directories and writes tooutput.txt
file (Note: You can use any of the above-discussed flags and the ls command. such as-s
,-R
-t
etc.)mkdir test
Creates a test directory in the current foldercd test
Changes the current working directory to testcd ..
ย Move up to one directorycd /
Takes to the root directory.cd
Takes to the home directorycd ~
Takes to the home directoryclear
Clears the terminaltouch file1.txt
Creates afile1.txt
file in the current working directorynano file1.txt
Opens thefile1.txt
file in the nano editorcat file1.txt
Shows the contents offile1.txt
less file1.txt
Shows less content offile1.txt
(Hit q to exit)mv file1.txt file2.txt
Renamesfile1.txt
tofile2.txt
cp file1.txt file2.txt
ย Copy the contents offile1.txt
tofile2.txt
cp file1.txt ~/dir2/file2.txt
Copy a file to a different pathrm file1.txt
Deletes a filermdir dir2
Deletes a directoryrm -R dir2
Deletes 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.txt
Search the text five infile1.txt
grep "five" blog/posts/file1.txt
Search the text five inblog/posts/file1.txt
grep -r "five" blog
Search in the blog directory and its child contents for text fivefind -name file1.txt
Search the filefile1.txt
in the current working directoryfind blog/posts -name 'file1.txt'
Finds the filefile1.txt
in theblog/posts
directoryfind blog/posts -name 'file*'
Finds the files prefix with โfileโ in theblog/posts
directoryfind -size +100M
Finds 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 -a
Displays Linux system informationuname -r
Displays kernel release informationuptime
Displays how long the system has been running and the load average.uptime -p
Same as above but displays the output in a pretty formatuptime -s
Displays how long the system has been uphostname
Displays the system hostnamehostname -I
Display all local IP addresses of the hostlast reboot
Displays the system reboot historydate
Displays the current datecal
Displays the calendarw
Displays the users who are onlinewhoami
Displays the logged-in userhistory
Shows the audit of the userwhich curl
Shows 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/passwd
Displays the list of users.getent passwd
Displays the list of users.The
cat /etc/passwd
command andgetent passwd
command 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/shadow
file).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/passwd
Displays the first two fields out of 7 fieldscut -d: -f1,2,3 /etc/passwd
Displays the first 3 fields out of 7 fieldssudo useradd test_user1
Creates a new user called test_user1sudo passwd test_user1
Sets the new password for user test_user1sudo useradd -m test_user2
Creates 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_user3
Creates a test_user3 user and also creates a test_user3 directory under opt directory.sudo useradd -u 1600 test_user4
Creates a test_user4 user with 1600 idid -u test_user4
Returns the id of the usersudo useradd -g users login_user1
Creates a new user and sets the login group to usersid -gn login_user1
Displays the group name of the usersudo useradd -g users -G user1,user2 login_user2
Creates a new user called login_user2 and sets the primary group as users and secondary groups as user1, and user2.id login_user2
Displays the userid, primary, and secondary group idssudo groupadd managers
Creates a new group called managerssudo chgrp managers file1.txt
Changes the group to managers onfile1.txt
sudo usermod -aG managers login_user2
Adds 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.
d
refers 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, an
all
+
adds permissions, -
removes permissions, =
adds new permissions and overrides existing
chmod o+w file1.txt
gives write access to the outside people on file1.txtchmod o-w file1.txt
removes write access for the outside people on file1.txtchmod g+wr file1.txt
gives read-and-write access to the groupschmod u=rwx file1.txt
gives 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.txt
gives execute access to the user, group, and otherschmod 750 file1.txt
gives read+write+execute access to users and read+execute access to groups and no permissions to outside people.chmod -R 777 posts
give 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.
ps
displays currently running user processesps -ef
displays currently running system processesps -ef grep 12693
displays the process information of 12693top
shows the processes running(hit q to exit)htop
alternative for the top (interactive process viewer)kill 24567
kills 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.
ifconfig
shows the network informationiwconfig
shows wireless informationping
google.com
returns the continuous response from the hostblkid
shows the storage informationdf
shows the available and unavailable disk spacelsusb
shows the connected devices listlspci
shows PCI informationwget
https://www.sample-videos.com/img/Sample-jpg-image-50kb.jpg
downloads 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 testDir
compress thetestDir
and createstestDir.tar
filetar xf testDir.tar
extract the contents of testDir.tartar czf testDir.tar.gz testDir
creates gzip compressed tar filetar xzf testDir.tar.gz
extract the compressed gzip filetar -tvf testDir.tar
displays the contents of the tar filetar -tvf testDir.tar.gz
displays the contents of gzip tar filetar -xf nodejs.tar.xs
Extracts 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.com
connect to the domain.com server with the root user (prompts for password)scp file1.txt
user1@domain.com
/tmp
copies the file securely to thetmp
folder in the domain.com serversudo scp -r
user1@domain.com:/var/www /temp
copies 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)