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 logs

Create nested folders:

mkdir -p proj/some/folder/

Create an empty file with the touch command:

touch somefile.txt

Update file’s timestamp: (file that was already created)

touch somefile.txt

List all files with details:

ls -l

Show hidden files:

ls -la

Sort files by time:

ls -lt

Copy files:

cp file1.txt somedir/

Copy a directory recursively:

cp -r /dir/youdir backupdir/

Rename a file:

mv onefile.txt onefile-old.txt

Move to another folder:

mv mylog.log somelogsdir/

Remove a file:

rm filetoremove.txt

Remove 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 +100M

The 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 -delete

Find file by owner:

find /home -user paco

Detect file type using the file command:

file oneimage.png

Check binary:

file /usr/bin/ls

You 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, stripped

Archiving & Compression

tar — bundle & compress:

Create a tar archive:

tar -cvf backit.tar proj/myfiles

The logic for the above is:

tar -<options> fileNameTobeCreated.tar <dir/filesYouWanttoCompress>

Extract a tar:

tar -xvf backit.tar

Create compressed tar.gz:

tar -czvf thisiscomp.tar.gz /etc/sshd

Extract tar.gz:

tar -xzvf tobeExtracted.tar.gz

cpio — copy files based on a list:

Archive files listed by find:

find /etc -type f | cpio -ov > thefiles.cpio

Extract a cpio file:

cpio -id < thefiles.cpio

dd command – block-level copying:

Clone a drive:

dd if=/dev/sda of=/dev/sdb bs=4M status=progress

Create a file of specific size:

dd if=/dev/zero of=thefile.img bs=1M count=100

Backup MBR (Master Boot Record):

dd if=/dev/sda of=mbr_backup.bin bs=512 count=1

Linux Compression Tools

Compress:

gzip afile.txt

Decompress the file:

gunzip afile.txt.gz

zip2 and bunzip2 examples. Higher compression, slower.

Compress:

bzip2 bigfile.iso

Decompress:

bunzip2 bigfile.iso.bz2

xz and unxz commands. Very high compression.

Compress:

xz somesql.sql

Decompress:

unxz somesqldb.sql.xz

Linux File Globbing (very useful!)

Globbing = smart wildcard matching. Very cool.

Match all .txt files:

ls *.txt

Match any file starting with “log”:

ls log*

Match files with one character:

ls thefile?.txt

This will match:

  • file1.txt
  • fileA.txt
  • fileb.txt
  • it will not match:
    • file12.txt

Match a range:

ls somefile[1-5].txt

Real-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 -delete

Brief 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

Leave a comment

Your email address will not be published. Required fields are marked *