Summary of Linux File Directories, Disk File Systems, and Compression Commands
File and Directory Management
Basic Commands
- • ls – List directory contents
ls # List files and directories in the current directory ls -l # Long format to display detailed information ls -a # Show all files (including hidden files) ls -la # Show detailed information and hidden files - • cd – Change working directory
cd /path/to/directory # Switch to the specified directory cd .. # Go back to the parent directory cd ~ # Switch to the user's home directory - • pwd – Display current working directory
pwd # Print the full path of the current working directory - • mkdir – Create a new directory
mkdir directory_name # Create a new directory named directory_name mkdir -p path/to/directory # Recursively create necessary parent directories - • rmdir – Remove empty directories
rmdir directory_name # Remove the empty directory named directory_name rmdir -p path/to/directory # Recursively remove empty directories - • cp – Copy files or directories
cp file1 file2 # Copy file1 to file2 cp -R /source_directory /destination_directory # Recursively copy an entire directory - • mv – Move or rename files or directories
mv old_name new_name # Rename file or directory mv /source/file /destination/ # Move file to another location - • rm – Remove files or directories
rm file_name # Remove the specified file rm -r directory_name # Recursively remove a directory and its contents rm -i file_name # Prompt for confirmation before deletion - • touch – Update file timestamp or create an empty file
touch file_name # If the file exists, update its access and modification times; if it does not exist, create an empty file
File Compression and Decompression
Basic Commands
- • gzip – Compress files
gzip file_name # Compress the file, generating file_name.gz - • gunzip – Decompress files
gunzip file_name.gz # Decompress file_name.gz, restoring it to the original file - • tar – Archiving tool that supports compression
tar -cvf archive.tar file1 file2 # Create archive file archive.tar tar -xvf archive.tar # Extract archive file archive.tar tar -zcvf archive.tar.gz file1 # Create archive file archive.tar.gz using gzip compression tar -zxvf archive.tar.gz # Decompress gzip compressed archive file tar -jcvf archive.tar.bz2 file1 # Create archive file archive.tar.bz2 using bzip2 compression tar -jxvf archive.tar.bz2 # Decompress bzip2 compressed archive file - • zip/unzip – Compress/Decompress .zip files
zip archive.zip file1 file2 # Compress files into archive.zip unzip archive.zip # Decompress .zip file
Basic Permissions
In Linux, file and directory permissions are divided into three categories: read (r), write (w), and execute (x), corresponding to the values 4, 2, and 1, respectively. Permissions can be assigned to three groups of users: owner (u), group (g), and others (o).
Basic Permission Commands
chmod – Change file mode
- • Symbolic Mode
chmod u+x,g+w,o-r file.txt # Add execute permission for the file owner, write permission for the group, and remove read permission for others chmod a+r file.txt # Add read permission for all users - • Absolute Mode
chmod 755 directory # Set directory permissions to rwxr-xr-x chmod 644 file.txt # Set file permissions to rw-r--r--
chown – Change file owner and group
- • Change the owner of a file
sudo chown newowner file.txt # Change the owner of file.txt to newowner - • Change the owner and group of a file
sudo chown newowner:newgroup file.txt # Change both owner and group
chgrp – Change file group
- • Change the group of a file
sudo chgrp newgroup file.txt # Change the group of file.txt to newgroup
Special Permissions
SUID, SGID, and Sticky Bit
- • SUID (Set User ID)
chmod u+s /path/to/program # Set the SUID bit - • SGID (Set Group ID)
chmod g+s /path/to/directory # Set the SGID bit - • Sticky Bit
chmod +t /path/to/directory # Set the sticky bit, usually for shared directories
ACLs (Access Control Lists)
ACLs provide a more granular way of managing permissions.
Setting ACL
- • Set user ACL
setfacl -m u:username:rwx file.txt # Set rwx permissions for a specific user - • Set default ACL
setfacl -d -m u:username:rwx directory # Set default permissions for new files and directories
View ACL
- • Use
<span>getfacl</span>command to view ACL informationgetfacl file.txt # Display the ACL settings of file.txt
Useful Tips
- • umask – Set the default permission mask for newly created files and directories
umask # View the current umask value umask 022 # Set the umask value, affecting the default permissions for subsequent file creation
Disk and File System Management
Basic Commands
- • df – Display disk space usage
df # Show disk usage for each mounted file system df -h # Display in a more readable format - • du – Display disk usage of directories or files
du # Show disk usage for each file and directory du -sh directory_name # Show total size of the specified directory - • fdisk – Partition table manipulation tool
sudo fdisk -l # List all partition tables - • mount – Mount file systems
mount /dev/sdb1 /mnt # Mount device /dev/sdb1 to /mnt - • umount – Unmount file systems
umount /mnt # Unmount the file system on /mnt
Software RAID Basics
In Linux, software RAID can be created and managed using the<span>mdadm</span> tool. Here are some commonly used commands and their usage examples.
Install mdadm
First, ensure that mdadm is installed on the system. If it is not installed, you can use the following commands to install it:
- • Debian/Ubuntu
sudo apt-get install mdadm - • CentOS/RHEL
sudo yum install mdadm
Create RAID Arrays
Create RAID 0 (Striping)
Assuming we have two disks /dev/sdb and /dev/sdc, to create a RAID 0 array:
sudo mdadm --create --verbose /dev/md0 --level=0 --raid-devices=2 /dev/sdb /dev/sdc
Create RAID 1 (Mirroring)
To create a RAID 1 array, also using the two disks /dev/sdb and /dev/sdc:
sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
Create RAID 5
For RAID 5, at least three disks are required:
sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sd[b-d]
Manage RAID Arrays
View RAID Status
To view the status of all RAID devices:
cat /proc/mdstat
Or use<span>mdadm</span>:
sudo mdadm --detail /dev/md0
Stop RAID Arrays
To stop a specific RAID device (e.g., /dev/md0):
sudo mdadm --stop /dev/md0
Delete RAID Arrays
Before deleting, stop the array first, then you can remove the superblock:
sudo mdadm --zero-superblock /dev/sdX
Here,<span>/dev/sdX</span> represents one of the physical disks involved in the RAID.
Configuration and Maintenance
Update Configuration File
After creating or modifying a RAID, it is recommended to update the<span>/etc/mdadm.conf</span> file so that the RAID configuration can be automatically loaded on system reboot:
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
Monitor RAID Health
You can monitor RAID health status by setting up email notifications:
Edit<span>/etc/mdadm.conf</span>, adding or modifying the following line:
MAILADDR [email protected]
Then restart the<span>mdmonitor</span> service (if it exists):
sudo systemctl restart mdmonitor
LVM (Logical Volume Management)
pvcreate – Create Physical Volume
- • Create a physical volume:
sudo pvcreate /dev/sdX1
vgcreate – Create Volume Group
- • Create a volume group:
sudo vgcreate myvg /dev/sdX1 /dev/sdY1
lvcreate – Create Logical Volume
- • Create a logical volume:
sudo lvcreate -L 10G -n mylv myvg
lvextend – Extend Logical Volume
- • Extend the size of a logical volume:
sudo lvextend -L +10G /dev/myvg/mylv - • Use resize2fs to adjust the file system size (for ext file systems):
sudo resize2fs /dev/myvg/mylv
Disk Quotas
Enable Quotas
- • Modify the /etc/fstab file to add usrquota and grpquota options for the file system where quotas need to be enabled:
UUID=xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /data ext4 defaults,usrquota,grpquota 0 2 - • Remount the file system to make the changes effective:
sudo mount -o remount /data
Set Quotas
- • Create a quota database file:
sudo quotacheck -cugm /data - • Edit user or group quotas:
sudo edquota -u username sudo edquota -g groupname