Jan 30, 2012

Shell Commands

Some frequently used shell commands

Forcibly remove all the files and subdirectories from a directory and then delete the directory

rm -rf mydir


List only the folders in a directory, not the files

ls -p | grep "/"

OR

ls -d */

OR {list ALL the directories recursively including hidden files}

du

Chopped version (print only the names and not the size before the names):

du|awk '{print $2}'

The most beautiful way of visualizing directory structure

tree -d

Chopped version of the above

tree -d -L 1



List the lines where one column is not equal to another column in the same csv.

awk -F\, '$1 != $4' some.csv



Batch Scripts

Open all similarly named pdf from the folder with evince one by one. One closes and the next opens.

for f in `ls somepatternofname*.pdf`; do echo $f; evince $f; done;


Rename all files in a folder that don't end in pdf to a single extension -".pdf".

for f in `ls * | grep -v pdf`;
do echo $f;
mv $f $f.pdf ;
done;


Rename a particular string to something else

for i in * ;
do j=`echo $i | sed 's#searchstring#replacestring#g' - ` ;
mv "$i" "$j" ;
done


Move all the folders in a particular directory to another directory or folder.

for f in *;
do
echo $f;
mv $f /new/directory ;
done


Rename the extension of multiple files in a folder
Here we are changing foo to bar

for f in *.foo;
do
base=`basename $f .foo`
mv $f $base.bar
done

0 Comments:

Post a Comment