I've some music that is all named 1 - (song author) - (song name).flac and I want to change the file name to just (song author) - (song name).flac then move it to a directory. Heres what I have so far:

while IFS='' read -r line || [[ -n "$line" ]]; do
 cp $line /home/user/Desktop/Music/$(cut -c3- "$line");
done < filelist.txt

filelist.txt contains a list of files that I want renamed and moved.

It doesn't work, but in a way I thought was interesting. I get the error bash: /bin/cp: Argument list too long, and after examining the contents of line after the cut, it just looks like random characters/character codes. Could someone tell me what I am doing wrong?

  • Can you show the $line? Where did filelist originate, perhaps it is unicode? – Paul Apr 4 '16 at 3:53
  • Also, the files are in the current folder according to this script. What criteria was used for them to be included in filelist.txt (there may be a way to do this without the file list). – Paul Apr 4 '16 at 3:54
  • filelist.txt was created with ls || grep flac > filelist.txt. I rather wouldn't include contents of $line. It is exactly like the contents of /dev/random – btf1998 Apr 4 '16 at 5:23
  • @btf1998 I'm afraid you did not create that file this way. What your command says: "run ls and only if it fails (almost never, called this way), run grep that looks for "flac" in stdin and write the output to filelist.txt". That is filelist.txt must have been created BEFORE you ran this command, by some other method. My guess would be: you copied one of the flacs somehow. – Gombai Sándor Apr 4 '16 at 7:24
  • small typo, ls | grep flac > filelist.txt was actually how the file was created. – btf1998 Apr 4 '16 at 12:22

cut uses a file, or stdin, and the switch -c is for characters and you most likely want fields instead, with space separators. Also, you have to enclose cp's second argument in quotes. And you need to specify -c5- to get rid of the :

while read -r line || [[ -n "$line" ]]; do
  cp $line "/home/user/Desktop/Music/$(echo "$line" | cut -d ' ' -f3-)"
done < filelist.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.