LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-30-2012, 09:58 PM   #1
damgar
Senior Member
 
Registered: Sep 2009
Location: dallas, tx
Distribution: Slackware - current multilib/gsb Arch
Posts: 1,949
Blog Entries: 8

Rep: Reputation: 203Reputation: 203Reputation: 203
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.
 
Old 07-30-2012, 10:11 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,307

Rep: Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743
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 ...

Last edited by chrism01; 07-30-2012 at 10:16 PM.
 
Old 07-30-2012, 10:16 PM   #3
damgar
Senior Member
 
Registered: Sep 2009
Location: dallas, tx
Distribution: Slackware - current multilib/gsb Arch
Posts: 1,949

Original Poster
Blog Entries: 8

Rep: Reputation: 203Reputation: 203Reputation: 203
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)."?
 
Old 07-30-2012, 11:13 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,307

Rep: Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743
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/

Last edited by chrism01; 07-30-2012 at 11:14 PM.
 
1 members found this post helpful.
Old 07-30-2012, 11:26 PM   #5
damgar
Senior Member
 
Registered: Sep 2009
Location: dallas, tx
Distribution: Slackware - current multilib/gsb Arch
Posts: 1,949

Original Poster
Blog Entries: 8

Rep: Reputation: 203Reputation: 203Reputation: 203

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!
 
Old 07-31-2012, 01:47 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 10:12 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Rename files with script sharathkv25 Programming 14 06-25-2007 03:00 AM
Script to Rename Many Files geeman2.0 Programming 3 04-05-2006 01:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration