linux commands
linux commands
1. sudo command
2. pwd command
3. cd command
4. ls command
5. cat command
6. cp command
7. mv command
8. mkdir command
9. rmdir command
10. rm command
11. touch command
12. locate command
13. find command
14. grep command
15. head command
16. tail command
17. diff, comm, and cmp commands
18. chmod command
19. chown command
20. man command
21. echo command
Linux Command
sudo is one of the most popular basic Linux commands that lets us to
perform tasks that require administrative or root permissions.
To navigate through the Linux files and directories, use the cd command.
Running this command without an option will take us to the home folder.
We need to keep in mind that only users with sudo privileges can execute it.
If we want to switch to a completely new directory, for example,
/home/username/Movies, we have to enter cd followed by the directory’s
absolute path:
cd /home/username/Movies
Here are some shortcuts to help us navigate:
cd ~[username] goes to another user’s home directory.
cd .. moves one directory up.
cd- moves to our previous directory.
To see other directories’ content, type ls followed by the desired path. For
example, to view files in the Documents folder, enter:
ls /home/username/Documents
5. cat command - Display file contents on the terminal
Use the cp command to copy files or directories and their content. Take a
look at the following use cases.
To copy one file from the current directory to another, enter cp followed by
the file name and the destination directory. For example:
cp filename.txt /home/username/Documents
To copy files to a directory, enter the file names followed by the destination
directory:
cp filename1.txt filename2.txt filename3.txt /home/username/Documents
To copy the content of a file to a new file in the same directory, enter cp
followed by the source file and the destination file:
cp filename1.txt filename2.txt
To copy an entire directory, pass the -R flag before typing the source
directory, followed by the destination directory:
cp -R /home/username/Documents /home/username/Documents_backup
7. mv command - Move or rename files in Linux
The primary use of the mv command is to move and rename files and
directories. Additionally, it doesn’t produce an output upon execution.
Simply type mv followed by the filename and the destination directory. For
example, we want to move filename.txt to the /home/username/Documents
directory:
mv filename.txt /home/username/Documents.
we can also use the mv command to rename a file:
mv old_filename.txt new_filename.txt
8. mkdir command - Command used to create directories in Linux
Use the mkdir command to create one or multiple directories at once and set
permissions for each of them.
Here’s the basic syntax:
mkdir [option] directory_name
The rm command is used to delete files within a directory. Make sure that the
user performing this command has write permissions.
Remember the directory’s location as this will remove the file(s) and we can’t
undo it.
Here’s the general syntax:
rm filename
The touch command allows we to create an empty file or generate and modify a
timestamp in the Linux command line.
For example, enter the following command to create an HTML file named Web
in the Documents directory:
touch /home/username/Documents/Web.html
15. head command - Return the specified number of lines from the top
The head command allows we to view the first ten lines of a text. Adding an
option lets we change the number of lines shown. The head command is also
used to output piped data to the CLI.
Here’s the general syntax:
head [option] [file]
For instance, we want to view the first ten lines of note.txt, located in the
current directory:
head note.txt
Below are some options we can add:
-n or –lines print the first customized number of lines. For example,
enter head -n 5 filename.txt to show the first five lines of filename.txt.
-c or –bytes prints the first customized number of bytes of each file.
-q or –quiet will not print headers specifying the file name.
16. tail command - Return the specified number of lines from the bottom
The tail command displays the last ten lines of a file. It allows users to check
whether a file has new data or to read error messages.
Here’s the general format:
tail [option] [file]
For example, we want to show the last ten lines of the file3.txt file:
tail file3.txt
20. man command - Access manual pages for all Linux commands
The man command provides a user manual of any commands or utilities we can
run in Terminal, including the name, description, and options.
It consists of nine sections:
Executable programs or shell commands
System calls
Library calls
Games
Special files
File formats and conventions
System administration commands
Kernel routines
Miscellaneous
To display the complete manual, enter:
man [command_name]
For example, we want to access the manual for the ls command:
man ls
Enter this command if we want to specify the displayed section:
man [option] [section_number] [command_name]
For instance, we want to see section 2 of the ls command manual:
man 2 ls
21. echo command - Print any text that follows the command
The echo command is a built-in utility that displays a line of text or string using
the standard output. Here’s the basic syntax:
echo [option] [string]
For example, we can display the text Linux Basic Commands by entering:
echo “Linux Basic Commands”.