I had a process that was pre-pending by design its PID to a file name string. But unfortunately, it caused a few unexpected problems when this handling process inadvertently terminated. I had to rename the files without the defunct pre-pended PID string and then reprocess them (several thousand).
Here is what I used as a quick and dirty procedure.
# csh
# ls
prependPID_filename_etc1 prependPID_filename_etc2 prependPID_filename_etc3 prependPID_filename_etc4
# foreach filename (prependPID*)
? mv $filename `echo $filename | sed 's/prependPID_//'`
? end
# ls
filename_etc1 filename_etc2 filename_etc3 filename_etc4
# zsh
# ls
prependPID_filename_etc1 prependPID_filename_etc2 prependPID_filename_etc3 prependPID_filename_etc4
# for i in prependPID*
for> mv $i `echo $i | sed 's/prependPID_//'`
# ls
filename_etc1 filename_etc2 filename_etc3 filename_etc4
This blog covers Unix system administration HOWTO tips for using inline for loops, find command, Unix scripting, configuration, SQL, various Unix-based tools, and command line interface syntax. The Unix OS supports tasks such as running hardware, device drivers, peripherals and third party applications. Share tips/comments. Read the comments. But most importantly: Read Disclaimer - Read Disclaimer.
Subscribe to:
Post Comments (Atom)
4 comments:
With bash, you could do:
for file in prependPID*; do
mv $file ${file##prependPID}
done
And it's all internal to the bash process; no need to call external utilities.
Not that it's a problem with 3 files, but it adds up in larger scripts.
> echo $SHELL
/bin/bash
> ls
file_garbage_1 file_garbage_2 file_garbage_3 file_garbage_4
> for f in file_garbage_*; do mv $f ${f/_garbage}; done
> ls
file_1 file_2 file_3 file_4
Would it be possible to just the opposite with either of these scripts?
I have a bunch of files in particular folders and I would like to prepend each file name within a particular folder with something (say "01." for instance) to differentiate it from similar files located currently in other folders. It would be very convenient to have a script where I could run that on the contents of a particular folder and have each file contained therein prepended.
At first I thought maybe mv *.txt 01.*.txt and that failed miserably. Then I thought perhaps mv $filename 01.$filename which failed equally miserably.
I think I'm close, but this is not my area.
Jameslsln,
Thanks for your question. I think this post might be able to help you.
http://www.mysysad.com/2007/05/add-strings-to-filenames-using-inline.html
additional: http://www.mysysad.com/search?q=filename+extension
Post a Comment