How to rename multiple files in Linux
From time to time, we may need to rename multiple files in Linux. It is quite frustrating to find out that mv old_name* new_name* is not working actually.
So after a few trials and errors, I’ve got the following script, which is really time saver for me.
This is what my files look like before the script.
$ ls -lth
total 12K
-rw-r–r– 1 username group 6 Aug 26 01:38 old_name_3.txt
-rw-r–r– 1 username group 6 Aug 26 01:38 old_name_2.txt
-rw-r–r– 1 username group 5 Aug 26 01:38 old_name_1.txt
I want to rename all those old_name to new_name. Here is the script to do that:
$ for nn in `ls -1 old*`; do NAME=`echo $nn | sed -e ‘s/^old_name/new_name/g’`; mv $nn $NAME; done
This is the aftermath.
$ ls -lth
total 12K
-rw-r–r– 1 username group 6 Aug 26 01:38 new_name_3.txt
-rw-r–r– 1 username group 6 Aug 26 01:38 new_name_2.txt
-rw-r–r– 1 username group 5 Aug 26 01:38 new_name_1.txt
I hope this script will be helpful for you as well. As always, feel free to leave a comment if you need any help.
Simple bash mail script
If you want to automate something on any Unix based servers and want to send you email in the bash script, here is the simple one.
#!/bin/bash
FROM="from_address@example.com"
TO=”to_address@example.com”
SUBJECT="Test Subject”
EMAILMESSAGE=”Test Message”
…
/bin/mail -s "$SUBJECT" "$TO" < $EMAILMESSAGE — -f $FROM
I hope this script will be helpful for you and feel free to leave a comment if you need any help.
How to check if the file is empty by using bash script
Here’s the simple script to check if the file is empty by using bash shell.
#!/bin/bash
TEMPFILE=”$REPORTPATH/testfile”
if [ -s $TEMPFILE ]; then
echo “File is not empty”
else
echo “File is empty”
fi
Of course you can substitute anything you desire in those echo lines.
Recent Posts
Recent Comments
- robost on How to install Vista bubble screen saver in Win XP
- Rick on Paint can create ico files
- Oliverdejohnson on Paint can create ico files
- Johnson Jones on Paint can create ico files
- Hussain on How to install fonts without admin right
Tags
Archives
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- November 2009
- July 2009
- May 2009
- April 2009
- January 2009
- October 2008
- September 2008
- August 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- November 2006
- February 2006
- January 2006
- December 2005

