Below are real-world examples practical Linux file management examples you can try on any test server.
Every command includes a quick explanation and a useful scenario. But, before trying the below Linux commands, I need to brew a great cup of specialty coffee at home.
Directory and File Basics
Create a folder for logs files:
mkdir logsCreate nested folders:
mkdir -p proj/some/folder/Create an empty file with the touch command:
touch somefile.txtUpdate file’s timestamp: (file that was already created)
touch somefile.txtList all files with details:
ls -lShow hidden files:
ls -laSort files by time:
ls -ltCopy files:
cp file1.txt somedir/Copy a directory recursively:
cp -r /dir/youdir backupdir/Rename a file:
mv onefile.txt onefile-old.txtMove to another folder:
mv mylog.log somelogsdir/Remove a file:
rm filetoremove.txtRemove a directory and contents:
rm -rf dirtoremove/⚠️ Always double check before running the above command!
Remove empty folder:
rmdir emptydir/If directory is NOT empty:
rm -rf notemptydir/Find all .log files using the find command:
find /sampledir/dir-to-search -name "*.log"The logic of the find command is:
find <location_to_search> -name "what-you-want-to-find"Find files bigger than 100 MB:
find / -type f -size +100MThe logic of the above find command:
find <locationToSearch> -<typeOfFile> <file> -<sizeOfFile> +<largerThanMB>Find and delete files older than 7 days:
find /somedir -type f -mtime +7 -deleteFind file by owner:
find /home -user pacoDetect file type using the file command:
file oneimage.pngCheck binary:
file /usr/bin/lsYou should get an output like this:
/usr/bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=ddcbef552f9c2ea0dbe2a1f38c312d26e0ac975b, strippedArchiving & Compression
tar — bundle & compress:
Create a tar archive:
tar -cvf backit.tar proj/myfilesThe logic for the above is:
tar -<options> fileNameTobeCreated.tar <dir/filesYouWanttoCompress>Extract a tar:
tar -xvf backit.tarCreate compressed tar.gz:
tar -czvf thisiscomp.tar.gz /etc/sshdExtract tar.gz:
tar -xzvf tobeExtracted.tar.gzcpio — copy files based on a list:
Archive files listed by find:
find /etc -type f | cpio -ov > thefiles.cpioExtract a cpio file:
cpio -id < thefiles.cpiodd command – block-level copying:
Clone a drive:
dd if=/dev/sda of=/dev/sdb bs=4M status=progressCreate a file of specific size:
dd if=/dev/zero of=thefile.img bs=1M count=100Backup MBR (Master Boot Record):
dd if=/dev/sda of=mbr_backup.bin bs=512 count=1Linux Compression Tools
Compress:
gzip afile.txtDecompress the file:
gunzip afile.txt.gzzip2 and bunzip2 examples. Higher compression, slower.
Compress:
bzip2 bigfile.isoDecompress:
bunzip2 bigfile.iso.bz2xz and unxz commands. Very high compression.
Compress:
xz somesql.sqlDecompress:
unxz somesqldb.sql.xzLinux File Globbing (very useful!)
Globbing = smart wildcard matching. Very cool.
Match all .txt files:
ls *.txtMatch any file starting with “log”:
ls log*Match files with one character:
ls thefile?.txtThis will match:
- file1.txt
- fileA.txt
- fileb.txt
- it will not match:
- file12.txt
Match a range:
ls somefile[1-5].txtReal-Life Sysadmin Examples Using Globbing
Delete all logs older than 7 days: (Cool way to keep your file system clean)
find /var/log -name "*.log" -mtime +7 -deleteBrief explanation of the above command:
/var/log → directory to search
-name "*.log" → match only *.log files
-mtime +7 → modified more than 7 days ago
–delete -> go ahead and delete the files
But, always remember this: Never delete blind! Check first:
find /var/log -type f -name "*.log" -mtime +7
