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:
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:
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...
Last edited by DukeNuke2; 02-24-2012 at 03:00 PM..
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.
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.
Yes, the blanks gt you right at the:
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.
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)
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)
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)
: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)
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)
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)
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)
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)
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)