All Linux commands fall into one of the following four categories:
- Shell builtins - Commands built directly into the shell with the fastest execution.
- Shell functions - Shell scripts (grouped commands).
- Aliases - Custom command shortcuts.
- Executable programs - Compiled and installed programs or scripts.
Here I show a list of typical Linux commands examples of how they work. I will present command in groups listed below:
- File and Directory Operations Commands
- File Permission Commands
- File Compression and Archiving Commands
- Process Management Commands
- System Information Commands
- Networking Commands
- IO Redirection Commands
- Environment Variable Commands
- User Management Commands
1. File and Directory Operations Commands
File and directory operations are fundamental in working with the Linux operating system. Here are some commonly used File and Directory Operations commands:
1a. pwd command
The pwd command (print working directory) is a shell builtin command that prints the current location.
pwd
1b. ls command
The ls command (list) prints a list of the current directory's content.
ls
Additional options:
-Show as a list
ls -l
-Show as list with hidden files
ls -la
-Show files with filesize
ls lah
1c. cd command
The cd command (change directory) is a shell builtin command for changing the current working directory:
cd <directory> (relative path)
cd <path> (full path)
1d. mkdir command
The mkdir command (make directory) creates a new directory in the provided location.
mkdir <Name_new_directory>
1e. rmdir command
Use the rmdir command (remove directory) to delete an empty directory.
rmdir <Name_direktory_to_delete>
1f. touch command
The primary purpose of the touch command is to modify an existing file's timestamp. To use the command, run:
touch <filename>
The command creates an empty file if it does not exist. Due to this effect, touch is also a quick way to make a new file (or a batch of files).
1g. cat command
The cat command (concatenate) displays the contents of a file in the terminal (standard output or stdout). To use the command, provide a file name from the current directory:
cat <file 1> <file 2>
! - To display with line number
cat -n <file 1> <file 2>
1h. cp command
The main way to copy files and directories in Linux is through the cp command (copy).
cp [Source_file] [Destination_file]
! - To prevent overide file
cp -i [Source_file] [Destination_file]
! - To copy directory recursively
cp -r [dir 1] [dir 2]
1i. mv command
Use the mv command (move) to move files or directories from one location to another.
mv <filename> ~/Documents/<filename>
mv <filename> <renamed_filename>
1j. rmdir/rm command
rm <filename> (path local or full path)
The rm command (remove) deletes files or directories. To use the command for non-empty directories, add the -r tag.
1.k head command
Use the head command to truncate long outputs.
head <file>
shows the first 10 lines of the <file>.
head -n 5 <filee>
displays the first 5 lines of the <file>.
1.l tail command
The Linux tail command does the opposite of head. Use the command to show the last ten lines of a file
tail <file>
tail -n 5 <file>
displays the last 5 lines of the <file>.
2. File Permission Commands
File permissions on Linux and Unix systems control access to files and directories. There are three basic permissions: read, write, and execute. Each permission can be granted or denied to three different categories of users: the owner of the file, the members of the file’s group, and everyone else.
2.a chmod command
Use the chmod (change mode) command to change file and directory permissions. The command requires setting the permission code and the file or directory to which the permissions apply.
chmod <permission> <file or directory>
Who
The “who” is a list of letters that specifies whom you’re going to be giving permissions to. These may be specified in any order.
u The user who owns the file (this means “you.”)
g The group the file belongs to.
o The other users
a all of the above (an abbreviation for ugo)
Permissions
Of course, the permissions are the same letters that you see in the directory listing:
r Permission to read the file.
w Permission to write (or delete) the file.
x Permission to execute the file, or, in the case of a directory, search it.
Example 1: Give read, write and execute permissions to the owner, read and execute permissions to the group and read and execute permissions to others
chmod 755 file.txt
chmod u=rwx,g=rx,o=rx file.txt
Example 2: Give read, write and execute permissions to the owner, read and execute permissions to the group and read and execute permissions to others
chmod 755 directory
chmod u=rwx,g=rx,o=rx directory
Example 3: Give read, write and execute permissions to the owner, read and execute permissions to the group and read and execute permissions to others
chmod -R 755 directory
chmod -R u=rwx,g=rx,o=rx directory
2.b chown command
The chown command (change ownership) changes the ownership of a file or directory. To transfer ownership, use the following command as sudo:
sudo chown <new owner name or UID> <file or directory>
(adduser - https://www.geeksforgeeks.org/adduser-command-in-linux-with-examples/)
! - https://phoenixnap.com/kb/linux-chown-command-with-examples
! - https://www.geeksforgeeks.org/addgroup-command-in-linux-with-examples/
Permission groups can be:
blank to signify the owner
g for group
o for others
a for all
Add or remove is
+ to add the permission
- to remove the permission
Permission type can be
r for read, copy and so forth
w for write, edit, delete and move
x to make it executable
Example 1: Change the ownership of a file. Txommand gives the ownership of the file to the root user and the root group.
chown root:root file.txt
Example 2: Change the ownership of a directory. The command gives the ownership of the directory to the root user and the root group.
chown root:root directory
Example 3: Change the ownership of a directory and all its subdirectories. The command gives the ownership of the directory and all its subdirectories to the root user and the root group.
chown -R root:root directory
The Chown and Chmod commands are very similar. The only difference is that the Chown command is used to change the ownership of a file or directory and the Chmod command is used to change the permissions of a file or directory.
2.c
chgrp
There is a third command called Chgrp that is used to change the group of a file or directory. The Chgrp command is used to change the group of a file or directory by using the following syntax:
chgrp [OPTIONS] GROUP FILE(s)
Example 1: Change the group of a file. Command gives the group of the file to the root group.
chgrp root file.txt
Example 2: Change the group of a directory. Command gives the group of the directory to the root group.
chgrp root directory
Example 3: Change the group of a directory and all its subdirectories. Command gives the group of the directory and all its subdirectories to the root group.
chgrp -R root directory
chgrp marketing file.txt – to change the group specified to a certain document
chgrp oracle /usr/database – to change the group specified to a certain directory
chgrp -R marketing /sales/2008 – to change the group specified to a certain directory recursively
2.d chown command
chown: This command changes file owner and group.
chmod 600 some_file
Here is a table of numbers that covers all the common settings. The ones beginning with “7” are used with programs (since they enable execution) and the rest are for other kinds of files.
Value
777 (rwxrwxrwx) No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
755 (rwxr-xr-x) The file’s owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
700 (rwx——) The file’s owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
666 (rw-rw-rw-) All users may read and write the file.
644 (rw-r–r–) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
600 (rw——-) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.
Here are some useful settings for directories:
Value Meaning
777 (rwxrwxrwx) No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting.
755 (rwxr-xr-x) The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users.
700 (rwx——) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.
chmod 755 file.txt
The above command is equivalent to the following command:
chmod u=rwx,g=rx,o=rx file.txt
2.4 umask command
The umask command in Linux is used to set default permissions for files or directories the user creates.
TBD
3. File Compression and Archiving Commands
Here are some file compression and archiving commands in Linux:
4. Process Management Commands
In Linux, process management commands allow you to monitor and control running processes on the system. Here are some commonly used process management commands:
5. System Information Commands
In Linux, there are several commands available to gather system information. Here are some commonly used system information commands:
uname - Print effective user name
whoami - Print effective user name
df - Report file system space usage
du - Estimate file space usage
free - Total usable memory
uptime - Utility for removing user
lscpu - Display CPU information
lspci - List PCI devices.
lsusb - List USB devices
exit - List USB devices
clear - List USB devices
passwd - Change user password
useradd - Create a new user or update default new user information
userdel - Utility for removing user
6. Networking CommandsDisplay network socket information.
In Linux, there are several networking commands available to manage and troubleshoot network connections. Here are some commonly used networking commands:
ss - Display network socket information.
7. IO Redirection Commands
In Linux, IO (Input/Output) redirection commands are used to redirect the standard input, output, and error streams of commands and processes. Here are some commonly used IO redirection commands:
Command | Description |
|---|
| cmd < file | Input of cmd is taken from file. |
| cmd > file | Standard output (stdout) of cmd is redirected to file. |
| cmd 2> file | Error output (stderr) of cmd is redirected to file. |
| cmd 2>&1 | stderr is redirected to the same place as stdout. |
| cmd1 <(cmd2) | Output of cmd2 is used as the input file for cmd1. |
| cmd > /dev/null | Discards the stdout of cmd by sending it to the null device. |
| cmd &> file | Every output of cmd is redirected to file. |
| cmd 1>&2 | stdout is redirected to the same place as stderr. |
| cmd >> file | Appends the stdout of cmd to file. |
8. Environment Variable Commands
In Linux, environment variables are used to store configuration settings, system information, and other variables that can be accessed by processes and shell scripts. Here are some commonly used environment variable commands:
Command | Description |
|---|
| export VARIABLE_NAME=value | Sets the value of an environment variable. |
| echo $VARIABLE_NAME | Displays the value of a specific environment variable. |
| env | Lists all environment variables currently set in the system. |
| unset VARIABLE_NAME | Unsets or removes an environment variable. |
| export -p | Shows a list of all currently exported environment variables. |
| env VAR1=value COMMAND | Sets the value of an environment variable for a specific command. |
| printenv | Displays the values of all environment variables. |
9. User Management Commands
In Linux, user management commands allow you to create, modify, and manage user accounts on the system. Here are some commonly used user management commands:
Command | Description |
|---|
| who | Show who is currently logged in. |
| sudo adduser username | Create a new user account on the system with the specified username. |
| finger | Display information about all the users currently logged into the system, including their usernames, login time, and terminal. |
| sudo deluser USER GROUPNAME | Remove the specified user from the specified group. |
| last | Show the recent login history of users. |
| finger username | Provide information about the specified user, including their username, real name, terminal, idle time, and login time. |
| sudo userdel -r username | Delete the specified user account from the system, including their home directory and associated files. The -r option ensures the removal of the user’s files. |
| sudo passwd -l username | Lock the password of the specified user account, preventing the user from logging in. |
| su – username | Switch to another user account with the user’s environment. |
| sudo usermod -a -G GROUPNAME USERNAME | Add an existing user to the specified group. The user is added to the group without removing them from their current groups. |
10. Shortcuts Commands
There are many shortcuts commands in Linux that can help you be more productive. Here are a few of the most common ones:
10.1: Bash Shortcuts Commands:
| Navigation | Description | Editing | Description | History | Description |
|---|
| Ctrl + A | Move to the beginning of the line. | Ctrl + U | Cut/delete from the cursor position to the beginning of the line. | Ctrl + R | Search command history (reverse search). |
| Ctrl + E | Move to the end of the line. | Ctrl + K | Cut/delete from the cursor position to the end of the line. | Ctrl + G | Escape from history search mode. |
| Ctrl + B | Move back one character. | Ctrl + W | Cut/delete the word before the cursor. | Ctrl + P | Go to the previous command in history. |
| Ctrl + F | Move forward one character. | Ctrl + Y | Paste the last cut text. | Ctrl + N | Go to the next command in history. |
| Alt + B | Move back one word | Ctrl + L | Clear the screen. | Ctrl + C | Terminate the current command. |
| Alt + F | Move forward one word. | | | | |
10.2: Nano Shortcuts Commands:
| File Operations | Description | Navigation | Description | Editing | Description | Search and Replace | Description |
|---|
| Ctrl + O | Save the file. | Ctrl + Y | Scroll up one page. | Ctrl + K | Cut/delete from the cursor position to the end of the line. | Ctrl + W | Search for a string in the text. |
| Ctrl + X | Exit Nano (prompt to save if modified). | Ctrl + V | Scroll down one page. | Ctrl + U | Uncut/restore the last cut text. | Alt + W | Search and replace a string in the text. |
| Ctrl + R | Read a file into the current buffer. | Alt + \ | Go to a specific line number. | Ctrl + 6 | Mark a block of text for copying or cutting. | Alt + R | Repeat the last search. |
| Ctrl + J | Justify the current paragraph. | Alt + , | Go to the beginning of the current line. | Ctrl + K | Cut/delete the marked block of text. | | |
| | | Alt + . | Go to the end of the current line. | Alt + 6 | Copy the marked block of text. | | |
10.3: VI Shortcuts Commands:
| Command | Description |
|---|
| cw | Change the current word. Deletes from the cursor position to the end of the current word and switches to insert mode. |
| dd | Delete the current line. |
| x | Delete the character under the cursor. |
| R | Enter replace mode. Overwrites characters starting from the cursor position until you press the Escape key. |
| o | Insert a new line below the current line and switch to insert mode. |
| u | Undo the last change. |
| s | Substitute the character under the cursor and switch to insert mode. |
| dw | Delete from the cursor position to the beginning of the next word. |
| D | Delete from the cursor position to the end of the line. |
| 4dw | Delete the next four words from the cursor position. |
| A | Switch to insert mode at the end of the current line. |
| S | Delete the current line and switch to insert mode. |
| r | Replace the character under the cursor with a new character entered from the keyboard. |
| i | Switch to insert mode before the cursor. |
| 3dd | Delete the current line and the two lines below it. |
| ESC | Exit from insert or command-line mode and return to command mode. |
| U | Restore the current line to its original state before any changes were made. |
| ~ | Switch the case of the character under the cursor. |
| a | Switch to insert mode after the cursor. |
| C | Delete from the cursor position to the end of the line and switch to insert mode. |
10.4: Vim Shortcuts Commands:
| Normal Mode | Description | Command Mode | Description | Visual Mode | Description |
|---|
| i | Enter insert mode at the current cursor position. | :w | Save the file. | v | Enter visual mode to select text. |
| x | Delete the character under the cursor. | :q | Quit Vim. | y | Copy the selected text. |
| dd | Delete the current line. | :q! | Quit Vim without saving changes. | d | Delete the selected text. |
| yy | Copy the current line. | :wq or :x | Save and quit Vim. | p | Paste the copied or deleted text. |
| p | Paste the copied or deleted text below the current line. | :s/old/new/g | Replace all occurrences of “old” with “new” in the file. | | |
| u | Undo the last change. | :set nu or :set number | Display line numbers. | | |
| Ctrl + R | Redo the last undo. | | | |