Introduction
Linux commands form the backbone of working with a Linux system. Mastering these commands allows you to efficiently navigate and manipulate files, directories, and system resources. In this chapter, we will cover the most essential commands for file and directory operations.
Table of Contents
Commands for File and Directory Operations
ls
The ls
command is used to list files and directories within the file system.
ls
-
ls -/
: Lists all files and directories at the root of the file system.
ls /
-
ls -l
: Provides a detailed listing including file permissions, number of links, owner, group, size, and timestamp.
ls -l
-
ls -a
: Shows all files, including hidden files.
ls -a
cd
The cd
command is used to change the current directory.
cd [directory]
-
cd ~
: Changes to the current user's home directory.
cd ~
-
cd ..
: Moves up one directory level.
cd ..
pwd
pwd
stands for "print working directory". This command displays the current directory.
pwd
touch
The touch
command is used to create an empty file or update the timestamp of an existing file.
touch file1.txt
cat
The cat
command is used to view the contents of a file.
cat [file_name]
-
cat file1.txt
: Displays the content of file1.txt.
cat file1.txt
mkdir
The mkdir
command is used to create new directories.
mkdir [directory_name]
-
To create a nested folder structure in Linux, you can use the mkdir command with the
-p
option like this:
mkdir -p directory-1/directory-2/directory-3/directory-4
The
-p
option tellsmkdir
to create the parent directories as needed.
rmdir
The rmdir
command is used to remove empty directories.
rmdir [directory_name]
cp
The cp command is used to copy files or directories.
cp [source] [destination]
-
cp file1.txt /home/user
: Copiesfile1.txt
to the/home/user
directory.
cp file1.txt /home/user
-
cp -r dir1 /home/user
: Recursively copies the directorydir1
and its contents to/home/user
. Recursively as used in this case means to copy everything in the specified directory (dir1
), including all its contents, subdirectories, and sub-subdirectories, and so on, all the way down.
cp -r dir1 /home/user
mv
The mv
command is used to move or rename files or directories.
mv [source] [destination]
-
mv file1.txt /home/user
: Movesfile1.txt
to the/home/user
directory.
mv file1.txt /home/user
-
mv
old_name.txt
new_name.txt
: Renamesold_name.txt
tonew_name.txt
.
mv old_name.txt new_name.txt
rm
The rm
command is used to remove files or directories.
rm [file_name]
-
rm file1.txt
: Deletesfile1.txt.
rm file1.txt
-
rm -r dir1
: Recursively deletes the directorydir1
and its contents.
rm -r dir1
Globbing
Globbing is a way to find files on your computer that match a certain pattern. Think of it like a search function, but instead of searching for a specific word or phrase, you can use special characters called "wildcards" to match many files at once. Globbing is useful because it saves you time and effort when working with files. You can use globbing to:
Find and delete many files at once
Copy or move many files to a new location
Run scripts or programs on many files at once
Wildcards
In globbing, "wildcards" are special characters that represent one or more characters in a file name. Here are the most common wildcards:
*
(asterisk): matches any characters (including none).[abc]
(square brackets): matches any character inside the brackets (e.g., a, b, or c).[a-z]
(square brackets with a range): matches any character in the range (e.g., a to z).
Below are example codes for each of the wildcards listed above:
-
*
(asterisk):
ls *.txt
The command above lists all files ending with .txt
-
[abc]
(square brackets):
ls [ab]*.txt
The command above lists files starting with a or b and ending with .txt (e.g., a_file.txt, b_file.txt)
-
[a-z]
(square brackets with a range):
ls [a-z]*.txt
The command above lists files starting with any lowercase letter and ending with .txt (e.g., a_file.txt, z_file.txt)
NB: These examples use the
ls
command to list files, but you can replacels
with other commands likerm
,cp
, ormv
to perform different actions on the matched files.
Conclusion
Mastering basic Linux commands is essential for navigating and managing a Linux system effectively. These commands form the foundation of working with files, directories, and system resources, empowering you to perform various tasks efficiently.
Feel free to leave comments and share this article. Follow my blog for more insights on Linux!
Top comments (0)