LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-19-2008, 09:29 PM   #1
pslacerda
Member
 
Registered: Nov 2008
Posts: 42

Rep: Reputation: 17
Question Spaces and escaped spaces


I want apply an command on files in a directory recursively. I used find, but he returns directories with spaces, and the command don't process it.

I tried change spaces to escaped spaces with sed s/ /\ /g and tr " " "\ ", but no results.

How do it?

Last edited by pslacerda; 12-19-2008 at 09:30 PM.
 
Old 12-19-2008, 10:02 PM   #2
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Hint: enclose filenames in quotes
 
Old 12-19-2008, 10:45 PM   #3
pslacerda
Member
 
Registered: Nov 2008
Posts: 42

Original Poster
Rep: Reputation: 17
So... how do it?

I'm newbie
 
Old 12-19-2008, 11:15 PM   #4
pslacerda
Member
 
Registered: Nov 2008
Posts: 42

Original Poster
Rep: Reputation: 17
I enclosed filenames with quotes on this way:

sed -e i\" -e a\" | sed -e :a -e N -e 's/\n//g' -e ta | sed 's/""/"\n"/g'

Certainly there is another way, what is?


obs:
I should edited the last post, or posted this new one? I'm new on forums
 
Old 12-19-2008, 11:52 PM   #5
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
you want to use an expression like:

command filename

except instead of "filename" you have "file name"

command file name

just tells you that directory file does not exist, so you use

command "file name"

instead.

It works on all the others too, so

command "filename"

is OK.

Can you see your way forward now?
 
Old 12-20-2008, 12:06 AM   #6
pslacerda
Member
 
Registered: Nov 2008
Posts: 42

Original Poster
Rep: Reputation: 17
You teach me: I need put quotes. But isn't only once, it's on every find returns, line by line. I want get the address (how to speak it?) of all files on a directory and put they on quotes.

find /home/shared return a lot of files, and put one by one will be terrible.

sorry if I didn't understood, english puzzle me.
 
Old 12-20-2008, 12:12 AM   #7
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
What is your current find command?
 
Old 12-20-2008, 12:19 AM   #8
pslacerda
Member
 
Registered: Nov 2008
Posts: 42

Original Poster
Rep: Reputation: 17
find /home/música
Code:
/home/música/
/home/música/Marcelinho da Lua; Seu Jorge
/home/música/Marcelinho da Lua; Seu Jorge/Marcelinho da Lua; Seu Jorge - Cotidiano.mp3
/home/música/John Lennon
/home/música/John Lennon/John Lennon - Stand by me.mp3
/home/música/John Lennon/John Lennon - Watching the Wheels.mp3
....
find /home/música | sed -e i\" -e a\" | sed -e :a -e N -e 's/\n//g' -e ta | sed 's/""/"\n"/g'
Code:
"/home/música/"
"/home/música/Marcelinho da Lua; Seu Jorge"
"/home/música/Marcelinho da Lua; Seu Jorge/Marcelinho da Lua; Seu Jorge - Cotidiano.mp3"
"/home/música/John Lennon"
"/home/música/John Lennon/John Lennon - Stand by me.mp3"
"/home/música/John Lennon/John Lennon - Watching the Wheels.mp3"
....
I want an more practical way.
 
Old 12-20-2008, 12:40 AM   #9
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
You could also try find's "exec" flag. For example, $ find . | xargs echo gives me quite the error (I have filenames with semicolons, apostrophes, spaces, and parentheses in them).

However, the following executes with no errors: $ find . -exec echo {} \;

The difference is that the second example lets find handle the translation instead of passing raw data through xargs. It's also nice that it alleviates the need for piped command chains. Give it a try with an innocent command, like the above 'echo' and see if it works for you!

Also, you might want to read more of the find man page -- there are various flags if you don't want to find directories, et cetera.
 
Old 12-20-2008, 01:01 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
If you use find to get a list of files, you can use the -print0 command to separate the file arguments with the null character. Then pipe the output of find to the xargs command. Use xargs -0 argument.

E.G.
find /podcasts -name "*.mp3" -print0 | xargs -0 id3info >podcast_tags

If you need to process the text of the arguments, or use something other than find for the filelist, you can use the `tr' command to convert newlines to nulls.
E.G.
find /podcasts -name "*.mp3" -exec md5sum '{}' \; | sort | uniq -w32 -d | cut -d' ' -f3- | tr '\n' '\0' | xargs -0 rm

This command line uses find to locate the MP3s in /podcasts/ including subdirectories. There may be duplicates. Taking the md5sum of each file and then sorting the output, identical files can be located because they have the same md5sum. The list of md5sums is sorted. The uniq command looks at the md5sum part on the line (the first 32 characters) and prints out duplicates (-d). The cut command then cuts out just the filename part of the lines. The filenames may contain spaces. The tr command converts the newlines to nulls, which is piped to xargs.

Another way is to use the -exec command in find.
find /podcasts -name "*.mp3" -exec id3info '{}' \; >podcast_tags

You can use find's `-printf' command to add the double quotes.
E.G.
find /podcasts -name "*.mp3" -printf "mv \"%p\" \"podcast_backups/%f\"" >migrate.sh

The `%p' alias will print out the full pathname. The `%f' alias will print out the file's basename.

Last edited by jschiwal; 12-20-2008 at 10:04 AM.
 
Old 12-20-2008, 09:29 AM   #11
pslacerda
Member
 
Registered: Nov 2008
Posts: 42

Original Poster
Rep: Reputation: 17
Poetics showed me the answer, but you showed me the way. I never before saw commands at this angle. And never used sort, uniq, tr successfully or find flags. You introduce me! Thanks.

On second example you are probably not paid attention and typed cut -d' ' -f3 instead cut -d' ' -f3-


thanks
 
Old 12-20-2008, 09:39 AM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
I don't see the difference between the two versions of `cut' you mentioned.

I just double checked this line which is correct.
Code:
find ./ -type f -name "*.mp3" -exec md5sum '{}' \; | sort | cut -d' ' -f3
I was just checking the cut command.

Here is a tip however. You can use "| tr -s ' ' |" to squeeze extra spaces between fields. This can come in handy if you are extracting fields from "ls -l".
 
Old 12-20-2008, 09:56 AM   #13
pslacerda
Member
 
Registered: Nov 2008
Posts: 42

Original Poster
Rep: Reputation: 17
because my musics contain spaces, so cut -d' ' -f3 here:

7a133d135f55da /música/album ficticio/musica imaginaria.mp3

will cut only the "/música/album", that is, the third field. With -f3- will cat /música/album ficticio/musica imaginaria.mp3, from the third field to the end, at least in my version.
 
Old 12-20-2008, 10:03 AM   #14
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
I got you. Yes, put a dash at the end of the -f3- option. I totally forgot about the spaces.
 
  


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
ps x without leading spaces LocoMojo Programming 3 03-09-2007 07:22 PM
Replacing spaces with a : xjlittle Programming 1 02-12-2007 09:42 AM
Way to many spaces LuderForChrist Linux - Newbie 4 06-25-2004 03:29 PM
spaces WindowsDefector Linux - Newbie 2 05-23-2002 03:03 AM
spaces in filenames ebone Linux - General 2 11-12-2001 12:56 PM

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

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