Some useful Linux shell commands

Below are some useful commands in Linux (applicable for RedHat Enterprise Linux, Fedora and CentOS) for regular server admin activities.

  • To find a word string in the files under a folder(s).

# find . | xargs grep ‘your-string’ -sl

  • To get the disk usages for the individual sub-directory run:

# ls -d /your/path/* | xargs du -sH

  • Replace a word string with another word string in all files in a folder.

Using ‘sed’ command:
# sed -i ‘s/ string1/ string2/g’ *.html

Using ‘find’ command:
# find ./ -type f -exec sed -i ’s/string1/string2/’ {} \;

  • When you deleting huge number of files in a folder, you might receive an error like “/bin/rm: Argument list too long”, do this instead “rm -rf” command:

# find . -name ‘*.ext’ | xargs rm

  • If you want to delete 20 days older files in folder, you can use the find command as below for your job.

This commands to list the files older than 20 days:
# find /path/folder1 -mtime +20 -type f -exec ls -d1 {} \;

Delete the files older than 20 days:
# find /path/folder1 -mtime +20 -type f -exec rm -rf {} \;
OR
# find /path/folder1 -mtime +20 -type f -ok rm -rf {} \;

You can change the older days count by increasing or decreasing the number and the file or folder by specify the “-type d” for folder.

  • Changing File or folder permission for multiple files and folder with the command
    chmod -R 777 /path/folder1/* is a risky affair, if you miss the path of folder or file the command would applicable to the root (/) folder and that can result a some wrong permission in system file/folder which leads a vulnerable system. Instead of using the “chmod” command directly, you can use it with find command for a safer modification.

For folders permission:
# find . -type d -exec chmod 555 {} \;

For files permission:
# find . -type f -exec chmod 444 {} \;

These are the most frequently used commands in Linux for day to day activities, thank you for your visit and please feel free to share any additional linux commands which you find useful :) .

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.00 out of 5)
Loading ... Loading ...

Tags:

2 Responses to “Some useful Linux shell commands”

  1. Hi
    Ankur

    I have visited your blog. It’s really a very good effort.
    I will add some value regarding oracle on linux.

    Thanks

  2. Good collection of useful commands!

    Thanks Much

Leave a Reply