Passing Arguments in Unix command line when using | symble

690 views Asked by At

I am trying to move all my video files that are in my pictures directory to my movies Directory. This is on a Mac by the way.

I thought I could simple Recurse through all my picture directories with an "ls -R"

Then I pipe that to grep -i ".avi" This give me all the movie files.

Now I pipe these values to "mv -n $1 ~/Movies" this I am hoping would move the files to the Movies folder.

I have a few Problems. 1. The "ls -R" does not list the path when listing the files. So I think I may fail to move the file. 2. I can not seem to get the file name to assign to the $1 in the mv command.

All together my command looks like this: Note I am running this from ~/Pictures

ls -R | grep -i ".avi" | mv -n $1 ~/Movies 

So right now I am not sure which part is failing but I do get this error:

usage: mv [-f | -i | -n] [-v] source target
   mv [-f | -i | -n] [-v] source ... directory

If I remove the 'mv' command I get a listing of avi files with out the path. Example Below:

4883.AVI
4884.AVI
4885.AVI
4886.AVI
4887.AVI
...

Any one have any ideas on how I can get the path in the 'ls' or how to pass a value in between the '|' commands.

Thanks.

5

There are 5 answers

3
Amit Kumar On

you can achieve this in many ways, one of it in my openion:

ls -R | grep -i ".avi" | while read movie
do
  echo " moving $movie"
  mv $movie ~/Movies/
done
0
Lucas Godoy On

It's better if you use the find command:

$ find -name "*.avi" -exec mv {} ~/Movies \;
1
Involution On

Use backticks

mv `ls *.avi` ~/Movies
1
nu11p01n73R On

The bash for loop can help you find all the avi files easily

shopt -s nullglob
for file in *.avi
do
   mv "$file" "$file"  ~/Movies/"$file"
done
0
vudangngoc On

you should create simple copy.sh like this

#!/bin/bash
cp $1 ~/Movies/

An run command ./copy.sh "$(ls | grep avi)"