Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
/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"
....
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.
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.
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.
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-
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".
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.