Bash script to rename files in a directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash script to rename files in a directory
# 1  
Old 02-24-2012
Power Bash script to rename files in a directory

Dear friends,

I have created a script to rename all files in a directory by appending the file name with username (who created the file), the date it was created. For example, "apple.doc" should be renamed to "johnFeb23apple.doc" where "john" is the owner and "Feb23" is file created date. It works fine with file names without spaces. but there are few issues I don't know how to fix.

First, Here's my script:

Code:
#!/bin/bash
for i in *
do
newname=$(ls -l ${i} | awk '{ print $3 $6 $7 $9 }')

    mv $i $newname
done

1. When it met a file with spaces, e.g. like "fff eee.xls" it gives the following error and won't rename that file:

Code:
ls: cannot access fff: No such file or directory
ls: cannot access eee.xls: No such file or directory
mv: cannot stat `fff': No such file or directory


2. I need to run this script in the parent directory (it's sub directory contains all files to be renamed). How do I make that change in my script? (adding path of sub directory where all files stored)

script is in "/tommy/" directory and files in "/tommy/data/" directory

Please help me. Thanks in advance... Smilie

Last edited by DukeNuke2; 02-24-2012 at 03:00 PM..
# 2  
Old 02-24-2012
You didn't quote the ${i} variable, so it split. Quote it "${i}"

Also, awk can't tell the difference between the spaces splitting the columns, and the spaces splitting the filename, causing problems.

Also, there's also no point running awk 9,000 times to process 9,000 lines. That's like making 9,000 phonecalls to say 9,000 words. awk can do it all in one go if it can do it at all.

But, I think shell read is better suited, since you can tell it exactly how many columns you want, and it won't split beyond that. Try and put 12 values into 9 columns, the last few columns will all get piled into the last variable unmodified. I really wish you could do that in awk, sometimes.

Code:
#!/bin/sh

cd /path/where/I/want/it/to/go

# The filename will get all put into the last variable, I
ls -l | while read A B C D E F G H I
do
        # Remove the 'echo' once you've tested it and found it does what you want.
        echo mv "$I" "$C$F$G$I"
done

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-24-2012
If you do not quote the white space, "$name" every reference, you need to use line feed as your delimiter, or if you have embedded linefeeds, more extreme tricks, like keying off the 'ls -l' non-name printout to pile up the right number of lines, tricky at EOF where there is no following line with an 'ls -l' prefix. C, PERL and such can handle each file name as one string without looking inside.
Code:
$ mkdir emb-ws
$ cd emb-ws
$ echo 'a space' >'a space'
$ echo 'two  space' >'two  space'
$ echo 'a   tab' >'a   tab'
$ echo 'a^Mcarriage-return' >'a^Mcarriage-return'
$  echo 'a
linefeed' >'a
linefeed'
$ ls | while read f; do   echo ">$f<"; done | cat -vte
> )
>a^Itab<$
>a<$
>linefeed<$
>a^Mcarriage-return<$
>a space<$
>two  space<$
$ ls -l |cat -vte
total 20$
-rw-rw-r-- 1 dpickett dpickett  6 Feb 24 15:09 a^Itab$
-rw-rw-r-- 1 dpickett dpickett 11 Feb 24 15:09 a$
linefeed$
-rw-rw-r-- 1 dpickett dpickett 18 Feb 24 15:09 a^Mcarriage-return$
-rw-rw-r-- 1 dpickett dpickett  8 Feb 24 15:09 a space$
-rw-rw-r-- 1 dpickett dpickett 11 Feb 24 15:09 two  space$
$

This User Gave Thanks to DGPickett For This Post:
# 4  
Old 02-24-2012
Quote:
Originally Posted by DGPickett
Yes, the blanks gt you right at the:
Code:
for i in *

The shell expands * and then divides the string by white space into a sequence of values.
Incorrect, * doesn't split on spaces by itself. * would be pretty useless if it did shell expansion inside itself. The right filenames end up in the $i variable.

But he doesn't quote the $i variable, causing it to split later.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-24-2012
Yes, I misspoke, and edited it back a bit. Life can get crazy if you have recursive evaluations, like with
Code:
for i in $(ssh -n hostx 'ls *')

, one of the many reasons I prefer the pipe
Code:
ssh -n hostx 'ls *' | while read i

This User Gave Thanks to DGPickett For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to make a bash script that goes through directory files and changes things

I'm trying to write a script in a directory that goes through the column the user specifies of 4 files that are inside the directory and calculates the min and the max values. This means that if the user specifies column 5, the script will go through column 5 of all 4 files and all that should give... (2 Replies)
Discussion started by: Eric1
2 Replies

2. Shell Programming and Scripting

Bash to rename files repeats previous filename in directory

In the below bash processes substitution, if there are 3 files in a directory /home/cmccabe/medex.logs/analysis.log, the filename variable is set to where these files are located. The code does execute, the problem is that if there is a renamed file in the output directory below, it gets... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Bash script to copy apache log files to client directory

Our Apache log files are written to a location on the server that we as clients have no access. Don't ask. Every month, I have to e-mail the administrator to have him manually copy our Apache log files to a directory in our file space. You can probably guess how efficient it is to do things this... (3 Replies)
Discussion started by: gregraven
3 Replies

4. Shell Programming and Scripting

Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute

:wall::wall::wall: Hi I have horrible script below, need help in renaming ls -l output into new filename format: Desired output: cp -pv original_path/.* newDirectory/owner_of_file.%dd%mm%y.file_extension.first_8_characters_of_original_filename localuser@localuser:~ vi... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

5. UNIX for Dummies Questions & Answers

Bash script to execute a program to rename files

I just can't figure it out , so please just give me a pice of advise how to: The existing Linux program foo2bar takes as its only argument the name of a single foo file and converts it to an appropriately-named bar file. Provide a script that when executed will run foo2bar against all foo... (4 Replies)
Discussion started by: raymen
4 Replies

6. UNIX for Dummies Questions & Answers

Bash script to rename all files within a folder...

Hi. I don't have any experience with making scripts in bash. I need a simple script to rename all files in a folder to the format file1.avi, file2.avi, file3.avi, and so on..... Please note that the original files have different filenames and different extensions. But they all need to be... (2 Replies)
Discussion started by: dranzer
2 Replies

7. Shell Programming and Scripting

Rename many files in a directory

Hi, I have around 100 xml file in a directory. I need to rename the files from .xml to .xml1. So i tried using the following command: mv *.xml *.xml1 but i am getting the following error mv: when moving multiple files, last argument must be a directory Try `mv --help' for more... (8 Replies)
Discussion started by: ananthi_ku
8 Replies

8. Shell Programming and Scripting

Simple BASH shell script to rename webcam jpg and copy into a new directory.

System: Ubuntu Intrepid Ibex I'm running webcamd as a sort of "security" program, but I need a script that will archive my webcam.jpg files. So, take the following file: /home/slag/www/webcam.jpg Rename it--preferably with a time stamp. Place it in say: /home/slag/www/history/ ... (4 Replies)
Discussion started by: robfindlay
4 Replies

9. Shell Programming and Scripting

Hello - new here - bash script - need to rename and zip files.

I'm working on a project that basically unzips three zip files. When these unzip they create about 70+ directories with subdirectories of year/month with about 3 to 9 pdf files in each directory. Basically, I'm needing to figure out a way to zip these pdf files up. for instance the script... (1 Reply)
Discussion started by: Aixia
1 Replies
Login or Register to Ask a Question