Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm a command line novice trying to figure out the appropriate command to execute the following within the Terminal in Mac OS X.

Assuming I've placed all files in the same directory, I want to isolate and move a selected set of directory files listed in a text file to a new directory, how do I:

  • read the selected list of filenames listed in a .txt file

  • find all matches by filename

  • isolate and move all file matches to a new directory

I've found this command to find (either or both) a filestring.filetype in a current directory and move all matches to a new directory:

find . -type f -iname "filestring.filetype" -print0 |
  xargs -0 -J % mv % ~/NewDirectory/

And I've found this tar command however it creates an archive copy of each filename listed in filename.txt

tar -cf - -T filename.txt | (cd /path/to/new/dir && tar -xvf -)

Any help is much appreciated!

The following solution was provided by a trusted source:

cat "filename.txt" | xargs -I % mv % ~/NewDirectory/
share|improve this question
    
Did any of those commands accomplish what you wanted? –  schaiba Feb 8 '13 at 22:18
    
Unfortunately, no. The tar command is closest however it creates an archive copy of the files listed in the filename and leaves the original directory files untouched. –  user2055921 Feb 8 '13 at 22:50
add comment

2 Answers

You could try this. Works with bash.

while read file
do
    mv -v -i "$file" ~/NewDirectory/
done < listoffiles.txt
share|improve this answer
    
Thank-you however this doesn't seem to execute in the Terminal –  user2055921 Feb 8 '13 at 23:18
    
I have bash 3.2 installed, doesn't seem work. Any other ideas? –  user2055921 Feb 8 '13 at 23:37
1  
You sure you typed it in correctly? I don't have access to any system with bash 3.2, but it also works in a busybox shell... -- also what's doesn't seem to work mean exactly? Error message? –  frostschutz Feb 8 '13 at 23:49
    
Thanks for getting back. I typed it letter-for-letter in the Terminal. I type each line, hit enter after each, after the last line, nothing happens as if the command isn't initiated. –  user2055921 Feb 8 '13 at 23:54
1  
That'd happen if the listoffiles.txt is empty. You could run it in a bash -x shell (debug output). Even if the file is empty it should show a + read file at the very least... otherwise I'm out of ideas, sorry. –  frostschutz Feb 9 '13 at 0:00
show 3 more comments

The find ... xargs solution you suggested works for me using Bash version 3.2.48(1)-release on Mac OS X 10.6.8.

mkdir -v -p ~/NewDirectory
find . -type f -maxdepth 1 -iname "*.pdf" -print0 |
   xargs -0 -J % mv -i -v % ~/NewDirectory

If you are going to use a text file containing the full paths of the files to be moved, make sure that each file path stored in the text file is terminated by a null byte. This way even newline characters in your file paths will not be of concern to you in the future.

# some tests
mkdir -v -p ~/NewDirectory
find "${PWD}" -type f -maxdepth 1 -print0 > listoffiles.txt
xargs -0 -n 1 ls -l <listoffiles.txt
# use a grep regex filter on file paths
grep -az 'bash' listoffiles.txt | xargs -0 -n 1 ls -l  
xargs -0 -n 1 sh -c 'echo mv -v -i "$1" ~/NewDirectory' _ <listoffiles.txt
xargs -0 -J % ls -ld % ~/NewDirectory <listoffiles.txt
xargs -0 -J % mv -i -v % ~/NewDirectory <listoffiles.txt

To debug your code in Bash use:

#help set
set -Eeuxv -o pipefail +H   # for debugging
trap 'printf "%s\n" "$?"; trap - EXIT' EXIT HUP INT QUIT PIPE TERM ERR
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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