The Linux file system follows a hierarchical structure. The root directory, denoted by /
, is the top - level directory. All other directories and files are organized beneath it. Some of the important directories in the Linux file system hierarchy are:
/bin
: Contains essential binary executables that are required for system boot and basic system operations./etc
: Holds system configuration files./home
: Home directories for user accounts are located here./var
: Stores variable data such as log files, mail spools, and temporary files.In Linux, each file and directory has associated permissions and ownership. There are three types of permissions: read (r
), write (w
), and execute (x
). These permissions are set for three different classes of users: the owner, the group, and others.
For example, if a file has permissions rwxr - xr - x
, it means the owner has read, write, and execute permissions, the group has read and execute permissions, and others also have read and execute permissions.
The ownership of a file or directory consists of the owner (a user account) and the group. You can change the ownership using the chown
command and the permissions using the chmod
command.
.
: Represents the current directory...
: Represents the parent directory./dev
: Contains device files, which are used to interact with hardware devices./proc
: A virtual file system that provides information about running processes and system status.pwd
(Print Working Directory): Displays the current working directory.pwd
cd
(Change Directory): Used to move between directories.# Move to the home directory
cd ~
# Move to the parent directory
cd..
# Move to a specific directory
cd /var/log
cat
(Concatenate): Displays the contents of a file.cat /etc/passwd
more
and less
: These commands are used to view large files page by page. less
is more advanced and has more features.less /var/log/syslog
nano
and vim
: Text editors for creating and editing files.# Open a file with nano
nano myfile.txt
# Open a file with vim
vim myfile.txt
ls
(List): Lists the contents of a directory.# List all files and directories in the current directory
ls
# List all files and directories including hidden ones
ls -a
# List files and directories in long format
ls -l
mkdir
(Make Directory): Creates a new directory.mkdir new_directory
rm
(Remove): Removes files and directories. Use the -r
option to remove directories recursively.# Remove a file
rm myfile.txt
# Remove a directory and its contents
rm -r new_directory
cp
(Copy): Copies files and directories.# Copy a file
cp source_file destination_file
# Copy a directory
cp -r source_directory destination_directory
mv
(Move): Can be used to move files and directories or rename them.# Move a file to a different directory
mv myfile.txt /new_location/
# Rename a file
mv old_name.txt new_name.txt
find
: A powerful command for searching files and directories based on various criteria such as name, size, and modification time.# Search for a file named "example.txt" in the current directory and its subdirectories
find. -name "example.txt"
# Search for files larger than 10MB
find / -size +10M
grep
: Used to search for a specific pattern in a file or a set of files.# Search for the word "error" in a log file
grep "error" /var/log/syslog
tar
: Used to create and extract archives.# Create a tar archive
tar -cvf archive.tar file1.txt file2.txt
# Extract a tar archive
tar -xvf archive.tar
gzip
and bzip2
: Used for compressing and decompressing files.# Compress a file
gzip myfile.txt
# Decompress a gzipped file
gzip -d myfile.txt.gz
chmod
: Changes the permissions of a file or directory. You can use either symbolic or numeric notation.# Add execute permission for the owner of a file
chmod u+x myfile.sh
# Set permissions to 644 (rw - r - r -)
chmod 644 myfile.txt
chown
: Changes the ownership of a file or directory.# Change the owner of a file to "new_user"
chown new_user myfile.txt
# Change the owner and group of a directory
chown new_user:new_group new_directory
.bashrc
for the Bash shell) to simplify frequently used commands.# Create an alias for listing files in long format
alias ll='ls -l'
#!/bin/bash
backup_dir="/backup"
source_dir="/home/user/documents"
tar -cvf $backup_dir/documents_backup.tar $source_dir
Regularly backup important data to prevent data loss. You can use tools like rsync
to perform incremental backups.
rsync -avz /home/user/documents/ /external_drive/backup/documents/
Use the df
command to check the disk space usage of file systems.
df -h
The -h
option makes the output human - readable.
Exploring file systems with the Linux command line is a valuable skill that offers great flexibility and control. By understanding the fundamental concepts, mastering the usage methods, following common practices, and implementing best practices, you can efficiently manage your files and directories, search for information, and ensure the safety of your data. With continuous practice, you will become more proficient in using the Linux command line for file system operations.