Results 1 to 10 of 16
Hi,
I'm sorry if this sound like a homework question. But I could not find a way on my own. My question is, is it possible to copy files 'except' ...
- 06-22-2009 #1
cp files _except_ few files
Hi,
I'm sorry if this sound like a homework question. But I could not find a way on my own. My question is, is it possible to copy files 'except' few files. i.e., say we have 26 files name them a, b, c, d... z. If I want to copy all these files from one directory to another except perhaps k, q, r, z.
One answer that came little close is this: https://lists.ubuntu.com/archives/ub...ly/088731.html But for some reason it is not the solution I want.
The same this may apply to mv command too.
Does anybody know how to do this?A candle looses nothing by lighting other candles. - Khalil Zibran.
Registered Linux User #490076
- 06-23-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,970
This will copy all but files that start with the letters (lower-case) k, q, r, and z to the destination directory (dest-dir).Code:cp [^kqrz]* dest-dir
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 06-23-2009 #3
ok here's what you do
do a listing of the all the files and place them in a copy file
now file copy will contain all the files. Open it up and delete the one's you don't want to copy and issue this commandCode:ls >copy
The solution pointed out by rubberman is nice but in case there are files with almost same name say K1 and K2 and only K2 need to be copied then removing K1 from the copy file will do the workCode:cp $(ll |grep -v PATTERN copy) destination/
Only if I could understand the man pages
Registered Linux user #492640
OS: RHEL4,5 ,RH 9,Ubuntu
- 06-28-2009 #4
Sorry guys, for two reasons:
1. For not replying to you for this long
2. Both methods don't work (or don't serve my purpose)
Rubberman,
Thats a regular expression involving caret, isn't it? Damn me...
Ok, it works but not the way I want. I wanted to leave not a file 'starting' with a character, but any file name I give. For eg: I did I actually wanted to copy all files except 3 and 4.Code:mkdir test test1 touch test/{1,2,3,4,435,s39,5,6} cp test/[^34]* test1/ ls test1 1 2 5 6 s39 bash-3.1$
Vickey,
What is that 'll'? is that 'long list' which is alias for 'ls -l'? And what is that PATTERN? I tried as below:
Again my intention was to copy all files except s39.Code:bash-3.1$ cp $(ls | grep -v s39 ~/test.txt) test1/ cp: cannot stat `1': No such file or directory cp: cannot stat `2': No such file or directory cp: cannot stat `3': No such file or directory cp: cannot stat `4': No such file or directory cp: cannot stat `435': No such file or directory cp: cannot stat `5': No such file or directory cp: cannot stat `6': No such file or directory bash-3.1$
I tried with 'cat' instead of 'll' and also fed test.txt to 'cat' instead of to 'grep'. Like this:It works but if use 'cp' along with this I get error as,Code:bash-3.1$ cat ~/test.txt | grep -v s39 1 2 3 4 435 5 6
What does 'cannot stat' mean? Btw, how to have multiple files in this PATTERN?Code:bash-3.1$ cp $(cat ~/test.txt | grep -v s39) test1/ cp: cannot stat `1': No such file or directory cp: cannot stat `2': No such file or directory cp: cannot stat `3': No such file or directory cp: cannot stat `4': No such file or directory cp: cannot stat `435': No such file or directory cp: cannot stat `5': No such file or directory cp: cannot stat `6': No such file or directory bash-3.1$
A candle looses nothing by lighting other candles. - Khalil Zibran.
Registered Linux User #490076
- 06-28-2009 #5Just Joined!
- Join Date
- Mar 2009
- Posts
- 31
I would try Rubberman's code again. It did exactly what you wanted it to do for me
I had these files in a directory (a b c d e f g h i j k l m n o p q r s t u v w x y z) and after I ran the script only these files where in the new directory (a b c d e f g h i j l m n o p s t u v w x y).Code:#!/bin/bash cp files/[^kqrz]* moved
- John
- 06-28-2009 #6yesCode:
What is that 'll'? is that 'long list' which is alias for 'ls -l'? And what is that PATTERN? I tried as below:
try the suggestion exactly as mentionedCode:ll is equivalent to ls -l it is a alias with comes predefined in Red Hat Linux
here you go
1) make a file called 'copy' which contains the list of all file by using. You must be present in the directory form where you want to copy to some destination
2)now file copy will contain all the files. Open it up and delete the one's you don't want to copyCode:ls >copy
3)now you are ready with the files u want to copy, issue this command
Code:cp $(ll |grep -v PATTERN copy) destination/
Though its a long procedure but will copy exactly the pattern it matchesCode:PATTERN keyword will search the file 'copy' and see what all to copy
If you want to shorten it you may give file name in the copy files with '*' after the name
For example if you wan to copy K11 K12 K13 but not K3 then
make an entry in the 'copy' file like this
Code:K1*
Only if I could understand the man pages
Registered Linux user #492640
OS: RHEL4,5 ,RH 9,Ubuntu
- 06-28-2009 #7I actually wanted to copy all files except 3 and 4. [/CODE]Code:
mkdir test test1 touch test/{1,2,3,4,435,s39,5,6} cp test/[^34]* test1/ ls test1 1 2 5 6 s39 bash-3.1$
In this case edit the copy file to have these contents
It worked out for meCode:the copy file ----------------------------------------------------- [^34]* #this will copy 1 2 5 6 s39 435 #this one overrides the ^3 option mentioned above s39 # Dito
Only if I could understand the man pages
Registered Linux user #492640
OS: RHEL4,5 ,RH 9,Ubuntu
- 06-28-2009 #8A candle looses nothing by lighting other candles. - Khalil Zibran.
Registered Linux User #490076
- 06-28-2009 #9
What's the point of exclude pattern -v then? If I have to delete files which I don't want to copy from the 'copy' file I can very well 'rm' or 'mv' those files from the directory itself and then use regular ' cp test/* test1/ ', right? No, it does not work.
I removed k1* here.Code:saivin@sv-gentoo ~ $ ls test >copy saivin@sv-gentoo ~ $ cat copy 1 2 3 4 435 5 6 k11 k12 k13 k3 s39 saivin@sv-gentoo ~ $ cp $(ls -l | grep -v k1* copy) test1/ cp: cannot stat `1': No such file or directory cp: cannot stat `2': No such file or directory cp: cannot stat `3': No such file or directory cp: cannot stat `4': No such file or directory cp: cannot stat `435': No such file or directory cp: cannot stat `5': No such file or directory cp: cannot stat `6': No such file or directory cp: cannot stat `s39': No such file or directory saivin@sv-gentoo ~ $ ls test1 saivin@sv-gentoo ~ $ saivin@sv-gentoo ~ $ cat copy 1 2 3 4 435 5 6 k11 k12 k13 k3 s39 saivin@sv-gentoo ~ $ vi copy saivin@sv-gentoo ~ $ cat copy 1 2 3 4 435 5 6 s39
Probably I'm not clear. See I have bunch of files in a directory. I want to copy all the files except few files. They need not have pattern like you said (eg. k1*). They can be completely different files like 'report.xls', 'fsflogo.png', 's39'. Now, how do I exclude these files from the group I'm copying to other folder. Forget about a bunch of different files, its not working for even one file. Try,Code:saivin@sv-gentoo ~ $ cp $(ls -l | grep -v k1* copy) test1/ cp: cannot stat `1': No such file or directory cp: cannot stat `2': No such file or directory cp: cannot stat `3': No such file or directory cp: cannot stat `4': No such file or directory cp: cannot stat `435': No such file or directory cp: cannot stat `5': No such file or directory cp: cannot stat `6': No such file or directory cp: cannot stat `s39': No such file or directory saivin@sv-gentoo ~ $ ls test1 saivin@sv-gentoo ~ $
I'm trying here to copy each file in test/ except file named s39.Code:mkdir test test1 touch test/{1,2,3,4,k11,k12,s39} ls test > copy cp $(ll | grep -v s39 copy) test1/
I think there is some thing wrong with '$()'. Your logic seems correct though. We are trying to copy the output of bunch of commands inside $() to destination. But '$()' is not giving correct output to 'cp' or 'cp' is not accepting it.A candle looses nothing by lighting other candles. - Khalil Zibran.
Registered Linux User #490076
- 06-28-2009 #10
My friend for a minute I would ask you to take off you thinking cap and do it the way I have shown. Why in the world are you replacing the keyword "PATTERN" with manual code. ____LET____ it remain like that , if you have some doubt please see the man page of grep there you will see the _____Keyword______ "PATTERN".Code:saivin@sv-gentoo ~ $ cp $(ls -l | grep -v k1* copy) test1/
Now here wat I did
Now the final directory doesn't contain files '3','4' as wanted.Code:vickey@vickey-laptop:~$ mkdir test test1 vickey@vickey-laptop:~$ touch test/{1,2,3,4,k11,k12,s39} vickey@vickey-laptop:~/test$ ls >copy vickey@vickey-laptop:~/test$ ls 1 2 3 4 copy k11 k12 s39 vickey@vickey-laptop:~/test$ #Now open the copy file vickey@vickey-laptop:~/test$ vi copy vickey@vickey-laptop:~/test$ cat copy [^34] s39 k* vickey@vickey-laptop:~/test$ cp $(ls -l | grep -v PATTERN copy) ../test1/ vickey@vickey-laptop:~/test$ cd ../test1 vickey@vickey-laptop:~/test1$ ls 1 2 k11 k12 s39
I hope this one clears it finally as I have given each step in systematic order.Only if I could understand the man pages
Registered Linux user #492640
OS: RHEL4,5 ,RH 9,Ubuntu


Reply With Quote
