Managing files and directories is a fundamental skill for anyone working in Linux. In cybersecurity, professionals frequently create, copy, move, search, and analyze files when investigating systems, configuring servers, or handling security tools.
This module introduces the essential commands used to manage files and directories efficiently in Linux.
Linux provides simple commands to create new files and folders.
touchThe touch command is used to create an empty file.
Example:
touch file1.txt
This creates a file named file1.txt in the current directory.
Create multiple files at once:
touch file1.txt file2.txt file3.txt
Common uses:
mkdirThe mkdir command is used to create directories (folders).
Example:
mkdir Documents
This creates a directory named Documents.
Create multiple directories:
mkdir folder1 folder2 folder3
Create nested directories:
mkdir -p Projects/Linux/Security
The -p option creates parent directories if they do not already exist.
Linux provides commands to manage file movement and deletion.
cpThe cp command copies files or directories.
Example:
cp file1.txt backup.txt
This creates a copy of file1.txt named backup.txt.
Copy a file into another directory:
cp file1.txt Documents/
Copy an entire directory:
cp -r folder1 folder2
The -r option means recursive, allowing directories and their contents to be copied.
mvThe mv command moves or renames files.
Move a file to another directory:
mv file1.txt Documents/
Rename a file:
mv file1.txt report.txt
Example:
mv oldname.txt newname.txt
rmThe rm command removes files.
Example:
rm file1.txt
Delete multiple files:
rm file1.txt file2.txt
Delete a directory:
rm -r folder1
The -r option allows deletion of directories and their contents.
⚠ Important:
Linux does not have a recycle bin for terminal deletion, so use rm carefully.
Linux provides several commands to display file contents directly in the terminal.
catThe cat command displays the entire content of a file.
Example:
cat file1.txt
It can also combine files:
cat file1.txt file2.txt
lessThe less command allows users to view large files page by page.
Example:
less logfile.txt
Benefits:
Exit with:
q
moreThe more command also displays files page by page but with fewer features than less.
Example:
more file1.txt
Linux provides powerful tools for locating files.
findThe find command searches for files in directories.
Example:
find /home -name file1.txt
This searches for file1.txt in the /home directory.
Search by file type:
find . -type f
Search directories:
find . -type d
locateThe locate command searches files quickly using a pre-built database.
Example:
locate file1.txt
Update the database:
sudo updatedb
Advantages:
findCompression reduces file size and allows multiple files to be packaged together.
This is important for:
tarThe tar command creates archive files.
Create an archive:
tar -cvf archive.tar folder1
Extract archive:
tar -xvf archive.tar
Options explained:
| Option | Meaning |
|---|---|
c |
Create archive |
x |
Extract archive |
v |
Verbose output |
f |
File name |
gzipThe gzip command compresses files.
Example:
gzip file1.txt
This creates:
file1.txt.gz
Decompress:
gunzip file1.txt.gz
zipThe zip command compresses files into a zip archive.
Example:
zip archive.zip file1.txt
Compress multiple files:
zip archive.zip file1.txt file2.txt
Extract zip file:
unzip archive.zip
In this module, students learned how to:
touch and mkdircp, mv, and rmcat, less, and morefind and locatetar, gzip, and zipThese commands are essential for Linux system administration, security analysis, and cybersecurity operations.