LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-15-2011, 09:25 AM   #1
TheNewGuy2936
LQ Newbie
 
Registered: Apr 2011
Location: Brooklyn NYC
Distribution: Redhat & CentOS
Posts: 23

Rep: Reputation: 6
Question Find Command


Hello everyone:

I have checked the man pages but I am still having issues understanding them. I have a directory on my server which contains files that have spaces in the names ex. This Is How It Looks.mp3

Can I use the find command to remove the spaces and replace it with _ underscore? If not, which command can I use to complete this task?

Thank you for your help!
 
Old 04-15-2011, 09:44 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Within one directory containing files you can do:

Code:
for i in *
do
  mv -n "$i" $(echo "$i" |  tr [:space:] _)
done
or instead of [:space:] meaning all white space you can use
Code:
' '
, meaning just a space.
 
1 members found this post helpful.
Old 04-15-2011, 10:10 AM   #3
TheNewGuy2936
LQ Newbie
 
Registered: Apr 2011
Location: Brooklyn NYC
Distribution: Redhat & CentOS
Posts: 23

Original Poster
Rep: Reputation: 6
When I ran that command I get the following:

jailshell-3.2$ for i in * do mv -n "$i" $(echo "$i" | tr [:space:] _);
>
>

I did it in different ways and I just keep getting that > simple which means I am doing something wrong. Do I have to run this as a script or I'm sure that it's that I am messing up
 
Old 04-15-2011, 11:49 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
H_TeXMeX_H made a mistake. It should say "done" at the end of that command (to mark the end of the loop). The ">" prompt means that your command isn't yet finished (use Ctrl+C to cancel it).

Also, remember that it will only work in the current directory. It will not go into subdirectories like find would.
 
Old 04-15-2011, 12:52 PM   #5
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
rename would also work:
rename -n 's/ /_/g' *
-n is a dry run, to test if one is happy. If all files end with mp3, *mp3 is probably a better wildchard.
The man-page (on Debian) is rather short.
 
Old 04-15-2011, 12:57 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by TheNewGuy2936 View Post
When I ran that command I get the following:

jailshell-3.2$ for i in * do mv -n "$i" $(echo "$i" | tr [:space:] _);
>
>

I did it in different ways and I just keep getting that > simple which means I am doing something wrong. Do I have to run this as a script or I'm sure that it's that I am messing up
Hi,

I do not see any syntactical mistakes in H_TexMex_H's solution. However, if you want to make a one liner then you will have to put a semicolon before the 'do'. You also forgot the trailing done, which also needs t be preceeded by a ';'.
Here is a similiar, bash only solution:
Code:
for f in *;do echo mv -n "$f" "${f// /_}";done
If the results look ok, then remove the 'echo' to make the changes actually happen.
Hope this helps.
 
2 members found this post helpful.
Old 04-15-2011, 12:58 PM   #7
TheNewGuy2936
LQ Newbie
 
Registered: Apr 2011
Location: Brooklyn NYC
Distribution: Redhat & CentOS
Posts: 23

Original Poster
Rep: Reputation: 6
That is fine because I only want it for that one directory. I tried to do what you said and now I get the following:

-jailshell-3.2$ for i in * do mv -n "$i" $(echo "$i" | tr [:space:] _); done
-jailshell: syntax error near unexpected token `done'

Do I have to put any other characters around the word done or anything?

Thank you!
 
Old 04-15-2011, 12:59 PM   #8
TheNewGuy2936
LQ Newbie
 
Registered: Apr 2011
Location: Brooklyn NYC
Distribution: Redhat & CentOS
Posts: 23

Original Poster
Rep: Reputation: 6
Wow sorry guys I did not see the other replies! I will try those now
 
Old 04-15-2011, 01:04 PM   #9
TheNewGuy2936
LQ Newbie
 
Registered: Apr 2011
Location: Brooklyn NYC
Distribution: Redhat & CentOS
Posts: 23

Original Poster
Rep: Reputation: 6
Ok, so I ran the command and this is what I got:

-jailshell-3.2$ for f in *;do mv -n "$f" "${f// /_}";done
mv: invalid option -- n
Try `mv --help' for more information.

I got an error like that for each file. When the echo command was in there like -jailshell-3.2$ for f in *;do echo mv -n "$f" "${f// /_}";done

It showed how it would look if you ran the command for real. When I removed the echo command I got those errors.
 
Old 04-15-2011, 01:06 PM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
You should use the "Code" tag, the little hash in the top of the editor, around your code so it looks more like this:

Code:
-jailshell-3.2$ for f in *;do mv -n "$f" "${f// /_}";done
mv: invalid option -- n
Try `mv --help' for more information.
 
Old 04-15-2011, 01:09 PM   #11
TheNewGuy2936
LQ Newbie
 
Registered: Apr 2011
Location: Brooklyn NYC
Distribution: Redhat & CentOS
Posts: 23

Original Poster
Rep: Reputation: 6
Oh ok cool I will remember for the next time, thank you! Sorry about that
 
Old 04-15-2011, 01:12 PM   #12
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by TheNewGuy2936 View Post
Oh ok cool I will remember for the next time, thank you! Sorry about that
No prob.

Also, you might find this small program helpful. Its at sourceforge and looks very cool.

http://stahlworks.com/dev/index.php?tool=deblank
 
Old 04-15-2011, 01:14 PM   #13
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by TheNewGuy2936 View Post
Ok, so I ran the command and this is what I got:

-jailshell-3.2$ for f in *;do mv -n "$f" "${f// /_}";done
mv: invalid option -- n
Try `mv --help' for more information.

I got an error like that for each file. When the echo command was in there like -jailshell-3.2$ for f in *;do echo mv -n "$f" "${f// /_}";done

It showed how it would look if you ran the command for real. When I removed the echo command I got those errors.
This is strange. Can you paste the output of
Code:
mv --help
The -n option tells 'mv' to not overwrite existing files. This should work equally:
Code:
for f in *;do if [[ ! -f "${f// /_}" ]];then mv "$f" "${f// /_}";fi;done
 
Old 04-15-2011, 01:18 PM   #14
TheNewGuy2936
LQ Newbie
 
Registered: Apr 2011
Location: Brooklyn NYC
Distribution: Redhat & CentOS
Posts: 23

Original Poster
Rep: Reputation: 6
YES!!! The above command worked! It removed the blanks and replaced them with the underscore just like I wanted! Just ask requested, here is the output anyway.

Quote:
-jailshell-3.2$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force do not prompt before overwriting
-i, --interactive prompt before overwrite
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-u, --update move only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:

none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups

Report bugs to <bug-coreutils@gnu.org>.
Thank you everyone for all of your help!! Have a great weekend!
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find a file in directories without using find command sikanders Linux - Newbie 14 08-06-2010 08:47 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
Single find command to find multiple files? thok Linux - Newbie 7 01-31-2009 04:45 PM
Using a single "Find" Command to find files bases on multiple criteria roboxooo Linux - Newbie 6 01-15-2009 04:13 AM
can't find my find command! how to replace? dave247 Debian 4 11-19-2008 10:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:28 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