Common Linux Shell Command Reference Print

  • 14

Finding Files larger than X in size

If you want to find files larger than 10mb:
find . -size +10240000c -exec du -h {} \;

Find files changed withing X days

If you want to find files changed within the last 5 days:
find . -type f -mtime -5 -exec ls -al {} \;

If you want to find files changed withing the last 30 days and append a tag to their names:
find . -type f -mtime -30 -exec mv {} "{}(CHANGED)" \;

And print them sorted with a nice format
find . -type f -mtime -30 -printf "%TY-%Tm-%Td %TT %p\n" | sort -r

How to Copy Files from One Folder to Another in Linux / Unix

If you want to copy all the files and folders from /path/folder/ to /other_path/folder/ you would...

Move to the Source Folder

cd /path/folder/

Copy All Files from Current Folder to Destination Folder

cp -R * /path/destination/folder/

Find Replace Text in Multiple Files

find ./ -type f -exec sed -i 's/string1/string2/g' {} \;

Mount New Disk

  1. Create Folder to Mount Too

    mkdir /path/folder
    ie: mkdir /media/disk1
  2. Mount the Disk

    mount -t {file-system-type} /disk/id /media/disk1

    -t indicates the type of file system - vfat = FAT32
    /disk/id indicates the disk identification - this can be attained by typing "fdisk -l" to list all available disks

Basic Directory Operations

  • List Directory Contents

    ls -al

  • Change Directory

    cd dirname

  • Make Directory

    mkdir dirname

  • Remove Directory (and all contents)

    rmdir dirname -Rf

Create TAR.GZ Gzip Compressed Tarball Files (For BACKUP, STORAGE, or TRANSFER)

  • Create TAR.GZ of all folder contents

    tar -czvf filename.tar.gz .
    -or-
    tar cvf - . | gzip > filename.tar.gz

  • Create TAR.GZ of just JPG images (for example)

    tar -czvf filename.tar.gz *.jpg
    -or-
    tar cvf - *.jpg | gzip > filename.tar.gz

  • Extract TAR.GZ contents (into current folder)

    tar -xzvf filename.tar.gz
    -or
    tar xvfz filename.tar.gz

How to Recursively Change Permissions - CHMOD Files and Folders

  • Recursively CHMOD Folders Only

    find . -type d -exec chmod 755 {} \;
  • Recursively CHMOD Files Only

    find . -type f -exec chmod 644 {} \;

How to Recursively Change Ownership - CHOWN Files and Folders

  • Recursively CHOWN Files and Folders

    chown user:group . -R

How to Recursively Rename Files

  • Recursively Rename

    find . -name "*.EXTA" | sed 's:\(.*\)\.EXTA:mv "\1.EXTA" "\1.EXTB":' | sh

    Where EXTA is the original file extension, and EXTB is the new file extension.
    This method uses Regular Expressions, and can be modified to perform any kind of filename substitution needed.

How to List Size of Folders

  • Scan folder content and show report of folder summary

    du -sh ./*

Was this answer helpful?

« Back