LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script to rename files, removing common name feature (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-rename-files-removing-common-name-feature-4175419464/)

damgar 07-30-2012 09:58 PM

Script to rename files, removing common name feature
 
We had a bit of a screwup at work last week (yes, I'll take the blame!) where a large number of files and directories were deleted from dropbox. There was no back up (except my own files, because I ALWAYS backup MY data, sorry, it's been a rough week :( ).

Anyway as it turns out the files weren't completely deleted, they survived in .dropbox.cache but all have been renamed. An example is like this:
Code:

Ashley Barnes (deleted 4fa2da8b-69620-cc228a29).pdf
What I would like to do is simply have a script go through and remove the (*) portion of the filename as well as the space that precedes the leading parentheses. I'm sure this is relatively simple for someone that knows what they are doing, but I have never really mastered sed or awk or whatever tools it is that is going to fix this.

Help is much appreciated.

chrism01 07-30-2012 10:11 PM

Like this ?
Code:

echo "Ashley Barnes (deleted 4fa2da8b-69620-cc228a29).pdf"|sed s/\ \(.*\)//
Ashley Barnes.pdf

PS: given that a space in an argument is usually interpreted as 2 args by most *nix tools I'd remove that space as well.
eg
Code:

echo 'Ashley Barnes.pdf'| sed s/\ /_/
Ashley_Barnes.pdf

I'm sure there are smarter solns coming ...

damgar 07-30-2012 10:16 PM

Yes that's the type of output I'm seeking. How would I go about scripting that to rename about 2000 files. The only common feature to all the files is " (deleted XXXXXX)."?

chrism01 07-30-2012 11:13 PM

Code:

# before
ls

Ashley Barnes2 (deleted 4fa2da8b-69620-cc228a29).pdf  Ashley Barnes (deleted 4fa2da8b-69620-cc228a29).pdf  test.txt  t.sh

# t.sh code
for file in *.pdf
do
        t=$(echo $file|sed s/\ \(.*\)//)
        echo $t
        mv "$file" "$t"
done

./t.sh

# after
ls
Ashley Barnes2.pdf  Ashley Barnes.pdf  test.txt  t.sh

HTH :)

Useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

damgar 07-30-2012 11:26 PM

Quote:

Originally Posted by chrism01 (Post 4741821)


THANK YOU! Yes that helps. I had to change *.pdf to *deleted*, but I almost feel rude even saying that! Works perfect and might get me out of the doghouse a bit! Well it works perfectly anyway, the doghouse is a different issue!

David the H. 07-31-2012 01:47 PM

No need to use sed in the above. The shell's built-in parameter substitution will be faster and more efficient.

Code:

file='Ashley Barnes (deleted 4fa2da8b-69620-cc228a29).pdf'

t=${file/ (deleted*)}
t=${t// /_}                #if you also want to replace spaces with underscores

mv "$file" "$t"



BTW, your system probably has some form of batch renamer available to you already, or at least easily installed.


All times are GMT -5. The time now is 02:45 AM.