Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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' ...
  1. #1
    Linux User saivin's Avatar
    Join Date
    Dec 2008
    Location
    Bengaluru, India
    Posts
    305

    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

  2. #2
    Linux Guru Rubberman's Avatar
    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
    Code:
    cp [^kqrz]* dest-dir
    This will copy all but files that start with the letters (lower-case) k, q, r, and z to the destination directory (dest-dir).
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493
    ok here's what you do
    do a listing of the all the files and place them in a copy file
    Code:
    ls >copy
    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 command
    Code:
    cp $(ll |grep -v PATTERN copy) destination/
    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 work
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  4. #4
    Linux User saivin's Avatar
    Join Date
    Dec 2008
    Location
    Bengaluru, India
    Posts
    305
    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
    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$
    I actually wanted to copy all files except 3 and 4.

    Vickey,
    What is that 'll'? is that 'long list' which is alias for 'ls -l'? And what is that PATTERN? I tried as below:
    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$
    Again my intention was to copy all files except s39.

    I tried with 'cat' instead of 'll' and also fed test.txt to 'cat' instead of to 'grep'. Like this:
    Code:
    bash-3.1$ cat ~/test.txt | grep -v s39
    1
    2
    3
    4
    435
    5
    6
    It works but if use 'cp' along with this I get error as,
    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$
    What does 'cannot stat' mean? Btw, how to have multiple files in this PATTERN?
    A candle looses nothing by lighting other candles. - Khalil Zibran.
    Registered Linux User #490076

  5. #5
    Just 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

    Code:
    #!/bin/bash
    
    cp files/[^kqrz]* moved
    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).

    - John

  6. #6
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493
    Code:
    What is that 'll'? is that 'long list' which is alias for 'ls -l'? 
    And what is that PATTERN? I tried as below:
    yes
    Code:
    ll is equivalent to ls -l
    it is a alias with comes predefined in Red Hat Linux
    try the suggestion exactly as mentioned
    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
    Code:
    ls >copy
    2)now file copy will contain all the files. Open it up and delete the one's you don't want to copy
    3)now you are ready with the files u want to copy, issue this command
    Code:
    cp $(ll |grep -v PATTERN copy) destination/
    Code:
    PATTERN keyword will search the file 'copy'  and see what all to copy
    Though its a long procedure but will copy exactly the pattern it matches
    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

  7. #7
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493
    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$
    I actually wanted to copy all files except 3 and 4. [/CODE]
    In this case edit the copy file to have these contents

    Code:
    the copy file
    -----------------------------------------------------
    [^34]* #this will copy 1 2 5 6 s39
    435    #this one overrides the ^3 option mentioned above
    s39    # Dito
    It worked out for me
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  8. #8
    Linux User saivin's Avatar
    Join Date
    Dec 2008
    Location
    Bengaluru, India
    Posts
    305
    Quote Originally Posted by ProgramIt View Post
    I would try Rubberman's code again. It did exactly what you wanted it to do for me

    Code:
    #!/bin/bash
    
    cp files/[^kqrz]* moved
    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).

    - John
    No, it worked for you coz you have used one-character files. Use it with multiple character files. Or preferably try on the file names given by me. For me it not only omitted 3 and 4 but also 435, which is not what I wanted.
    A candle looses nothing by lighting other candles. - Khalil Zibran.
    Registered Linux User #490076

  9. #9
    Linux User saivin's Avatar
    Join Date
    Dec 2008
    Location
    Bengaluru, India
    Posts
    305
    Quote Originally Posted by vickey_20 View Post
    ...
    try the suggestion exactly as mentioned
    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
    Code:
    ls >copy
    2)now file copy will contain all the files. Open it up and delete the one's you don't want to copy
    3)now you are ready with the files u want to copy, issue this command
    Code:
    cp $(ll |grep -v PATTERN copy) destination/
    PATTERN keyword will search the file 'copy' and see what all to copy.
    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?
    Quote Originally Posted by vickey_20 View Post
    Though its a long procedure but will copy exactly the pattern it matches
    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*
    No, it does not work.
    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
    I removed k1* here.
    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 ~ $
    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:
    mkdir test test1
    touch test/{1,2,3,4,k11,k12,s39}
    ls test > copy
    cp $(ll | grep -v s39 copy) test1/
    I'm trying here to copy each file in test/ except file named s39.

    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

  10. #10
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493

    Talking

    Code:
    saivin@sv-gentoo ~ $ cp $(ls -l | grep -v k1* copy) test1/
    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".



    Now here wat I did
    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
    Now the final directory doesn't contain files '3','4' as wanted.
    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

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...