Unix commands that make life easier

1. Changing files and folder name automatically

find . -name ‘*.filex’  -type f  -exec perl -pi -e ‘s/SEARCHTEAM/TOBEREPLACEDWITH/g’ \{\} \;

Here *.filex are all files of the type filex, we can replace it with *.txt etc.

2. Search for a KEYWORD in files:

There are two ways of searching a “Keyword” in the directory /etc

find /etc -name “*” -print|xargs grep “KEYWORD

find /etc -type f -exec grep -l ‘KEYWORD‘ {} \;

3. A shell script to block/disable USB in Linux

#! /bin/sh

#The script name is block_usb.sh

#Copy th etext into a file abd execute

mkdir /usr/local/usb

cd /lib/modules/

pwd >/usr/local/usb/path1.txt

find . -name ‘usb-storage.ko’ >/usr/local/usb/path2.txt

find . -name ‘usb-storage.ko’ -exec mv {} /usr/local/usb \;

/sbin/rmmod usb-storage

printf “To enable usb please use the following: \nRefer Path1 from path1.txt and then Path2 from path2.txt. \nThere may be two paths in path2.txt, so please follow the process for both the paths similarly. \n cd Path1 \n cd /Path2 (ignore *.ko file) \n cp /usr/local/usb/usb-storage.ko . \n restart” >/usr/local/usb/readme.txt

This script creates a folder ‘usb’ in /usr/local with related instructions.

4. Run command at boot time

cp <script-file> /etc/init.d

ln -s /etc/init.d/ /etc/rc.d/rc5.d/S50

ln -s /etc/init.d/ /etc/rc.d/rc5.d/K50

5. To know the size of folder/directories only

du -h –max-depth=1

6. Split a file based on pattern

csplit file.txt /”pattern”/5 {*}

7. Count number of a word in a file

cat file_name | tr ‘ ‘ ‘\n’ | grep “word_to_be_searched” | wc -l | sed ‘s/^[ \s]*//’

grep -o word2bcounted filename | wc -w

8. Changing data inside file automatically

find . -name ‘file.txt’  -type f  -exec perl -pi -e ‘s/PATTERN/TOBEREPLACEDWITH/g’ \{\} \;


2 thoughts on “Unix commands that make life easier

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s