LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Spaces and escaped spaces (https://www.linuxquestions.org/questions/linux-newbie-8/spaces-and-escaped-spaces-691807/)

pslacerda 12-19-2008 08:29 PM

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? :)

Simon Bridge 12-19-2008 09:02 PM

Hint: enclose filenames in quotes

pslacerda 12-19-2008 09:45 PM

So... how do it?

I'm newbie

pslacerda 12-19-2008 10:15 PM

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 :)

Simon Bridge 12-19-2008 10:52 PM

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?

pslacerda 12-19-2008 11:06 PM

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. :(

Poetics 12-19-2008 11:12 PM

What is your current find command?

pslacerda 12-19-2008 11:19 PM

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.

Poetics 12-19-2008 11:40 PM

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.

jschiwal 12-20-2008 12:01 AM

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.

pslacerda 12-20-2008 08:29 AM

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:D

jschiwal 12-20-2008 08:39 AM

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".

pslacerda 12-20-2008 08:56 AM

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.

jschiwal 12-20-2008 09:03 AM

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


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