LinuxQuestions.org
Visit Jeremy's Blog.
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, 10: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: 201Reputation: 201Reputation: 201
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, 11:11 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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 11:16 PM.
 
Old 07-30-2012, 11:16 PM   #3
damgar
Senior Member
 
Registered: Sep 2009
Location: dallas, tx
Distribution: Slackware - current multilib/gsb Arch
Posts: 1,949
Blog Entries: 8

Original Poster
Rep: Reputation: 201Reputation: 201Reputation: 201
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-31-2012, 12:13 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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-31-2012 at 12:14 AM.
 
1 members found this post helpful.
Old 07-31-2012, 12:26 AM   #5
damgar
Senior Member
 
Registered: Sep 2009
Location: dallas, tx
Distribution: Slackware - current multilib/gsb Arch
Posts: 1,949
Blog Entries: 8

Original Poster
Rep: Reputation: 201Reputation: 201Reputation: 201

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, 02:47 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 09:00 AM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 11:12 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM
Rename files with script sharathkv25 Programming 14 06-25-2007 04:00 AM
Script to Rename Many Files geeman2.0 Programming 3 04-05-2006 02:45 PM


All times are GMT -5. The time now is 10:10 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration