With Terminal: Find and copy a list of files

Hello to all readers 😀

After a failed vacation, I deign to write these lines. It turns out that my 3gb mp32 player was damaged and the only way to recover it is by formatting it, that's not a problem, what actually mortified me was thinking about the time it would take for me to select and copy the music collection that I had so much taste had made.

In that I remembered how powerful the terminal can be for these cases and after a few minutes of reading documentation (I'm not the best at bash 😛) I got to this method:

1. I made a list with all the files in the player:

dir / media / musicplayer> music_list

2. After generating that list, we apply the following command:

 cat music_list | while read f; do find. -name "$ f" -exec cp {} / media / musicplayer \ ;; done

cat read the list, then, find is in charge of looking for them and finally cp copies it to the destination folder, in a cycle until the last line of the file is read.

and ready!

The way the console works is moving, right?

I'm sure it can be useful in more productive activities (and less trivial than my player hahaha), and of course there would be other ways to reach the same goal (maybe using Rsync o xargs) but it seemed to me that this was the simplest way to achieve it.

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

46 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *



  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   elav said

    Oh! Great 😀

  2.   Vampii said

    Ehhh…. »Dir» ???? Did you read correctly and did you use the command "dir"? WTF !!! The command is "ls" ... DIR is a DOS command and not * nix (or GNU / Linux) ... I know I'm an old Slacker but isn't there an error there?

    For the rest, excellent! a good way to be lazy, is by using the console

    1.    eliotime3000 said

      We are the same at that point of DIR, since in POSIX LS is used as its replacement.

      To err is of humans.

    2.    pepper said

      `man dir`

      1.    elav said

        Exact. DIR also exists in our distributions 😛

    3.    elav said

      In fact, when I run:

      man dir

      Look what I get at the end:

      AUTHOR Written by Richard M. Stallman and David MacKenzie.
      1.    Giskard said

        Look at you! But that's recent, isn't it? Because a few years ago, when I started using Linux, I used to write dir instead of ls out of habit and I painted one!

      2.    eliotime3000 said

        I withdraw what I omenté from DIR.

    4.    helena_ryuu said

      I said it myself, I am a complete n00b in bash xDDD sorry if I offended you with dir hahaha, really, I know it was ls, but at that moment I only remembered to say ... it was just a list of files 😛

      1.    eliotime3000 said

        Do not worry. In the same way, I am not so recurrent to bash, so we are at hand.

        1.    eliotime3000 said

          And by the way, I suggest you try Iceweasel (it's in AUR and the logo is prettier than Firefox's).

          : )

          1.    helena_ryuu said

            I'll give it a try ^^

          2.    elav said

            In the end it is the same. Why install Iceweasel when we already have Firefox in the repositories?

          3.    cookie said

            Eliot, it is the same as Firefox but with an uglier logo ¬¬ and then apart is in the AUR

          4.    eliotime3000 said

            It depends on how you see it, because so far I have not seen a new version of the Iceweasel logo with a "flat" design or with an option to send my data to Mozilla to see if it works well or not.

            That the Iceweasel logo is not to your liking is fine, I respect your decision.

      2.    Sam burgos said

        Do not worry that in exchange for breaking your head with bash you no longer want to do things in other languages ​​since you can not find where 😀 (my particular case), apart from that it is good to use it to operate in several distros

        As for your script, I took the trouble to tidy it up a bit and put it in a pastebin, although I would like to know some parameters for what they are for because I don't understand them much (I will investigate when I am at home since in my work I am tied hands and feet)

        Script -> http://pastebin.com/3c1zwS5y

        1.    helena_ryuu said

          Indeed, you have left it more orderly, I was going to do it like this, but…. for some reason I remember the arrangements (how much they make me suffer in the programming class) so I better left it in a single line hahaha

          1.    Sam burgos said

            Well, it is helpful to order the codes because otherwise it is difficult to understand them later (as a documentor / analyst / programmer that is a great pain and I tell you from my own experience)

            And some other thing with bash I offer to help with pleasure, I am not a master in that either but of the languages ​​that I like to use (and that I have more dusted: P) that is one of them, although for someone who, supposedly, is a novice bash you left me thinking what the code itself does 😉 😛

    5.    haobaobatusai said

      test by running dir

    6.    ecnomus said

      No mistake, you can also use the DIR command in Linux, it could be more elegant to use LS but to get a list of DIR files it works fine because currently most distributions have the default alias dir = 'ls -l by default.

  3.   eliotime3000 said

    I liked the tip. I hope more tutorials will be written about the many uses of the GNU / Linux console, which really outperforms the Windows one.

    1.    KZKG ^ Gaara said
      1.    eliotime3000 said

        Sharing and adding to favorites.

  4.   D.Adrian said

    Excuse my ignorance, but the truth is that I did not understand almost anything: the first command, the dir, makes a list of the files that are inside a folder, then, according to what I understand, the find will search one by one those files described in that list and copy them to another folder. I ask, why do you need to search for them if I already know that they are in / media / musicplayer, and it is only enough to copy that folder to another destination.
    It will not be that in / media / musicplayer there are all kinds of files and I want to find, let's say only the mp3s and copy them to another side?
    If someone could better explain what the problem is and what I want to do?
    Thank you and excuse me.

    1.    helena_ryuu said

      Well as you say, if it is easier to just copy the files to another side, the problem I had is that the files were unreadable, in linux, and in windows, so I only had to format it 😛, and fill it again I gave laziness, for that reason.
      As for what you say about filtering, I think you should add the -o and -name flag to the find command, something like this:
      find. \ (-name '* .jpg' -o -name '* .png' -o -name '* .JPG' -o -name '* .PNG' -o -name '* .JPEG' -o -name ' * .jpeg '\)
      in that case it would filter only * .jpg, * .JPG, * .png, * .PNG, * .jpeg and * .JPEG files

  5.   hexborg said

    Congratulations. You have used the terminal as it is really intended to be used.

  6.   Dcoy said

    What would our lives be without the powerful console: 3 ... .

  7.   He passed through here said

    very good, I have
    alias listen = 'echo "$ (find $ (pwd) -iname" * .MP3 ″) "| xargs -n1 -I aa ln -s "aa" $ HOME / listen / '

    1.    davidlg said

      very similar to the one I use, to move the files that the transmission has finished
      [code] find -iname "* .avi" | xargs -i mv {} / home / wizord / Videos /
      [/ Code]

  8.   Uriel said

    Excuse the ignorance, but I didn't quite understand the order either and therefore I don't know if it applies or not to what I want to do.
    I have a 160th generation 6gb iPod, which since when I want to make a backup before it is too late, however I don't know if with what you write I can separate the music according to the playlists, since there are many songs with it. Same name but on different albums or artists, can I explain myself? that's why I can't just copy the .mp3s to a single folder because it would conflict with songs with the same name. Can I make my backup with the commands you propose or is there any other alternative?
    I hope you can help me, thank you !!

    ps: I thought to install Rockbox to it, but it is not compatible with my ipod according to the developer page
    pd2: gtkpod apparently also copies only the .mp3 without order, or maybe I didn't know how to do it.

  9.   truko22 said

    It's great 😀

  10.   eliotime3000 said

    Well, everyone is free to use the browser that one wants.

    I am using Iceweasel on Debian with the release branch of mozilla.debian.net, and I am not against those who use that browser.

    I only suggested that @helena try Iceweasel because Iceweasel identifies it for having a pretty cute logo (which many don't like), but don't be alarmed.

    Anyway, I'm using Iceweasel on Debian because I am lazy to install Firefox and I already have Firefox on Slackware and it works wonders.

    1.    elav said

      Clear. What I mean is that Firefox is already packaged in the repository. Iceweasel would have to be installed from AUR with all the risks that this entails.

      1.    eliotime3000 said

        Ah good. Of course, it would be advisable to replace Firefox with Iceweasel, since I have tried having both browsers, but both cannot be run at the same time.

        As soon as I finish experimenting with Slackware, I will make a virtual machine in which I will install Arch with Iceweasel and a lightweight interface (XFCE or LXDE).

    2.    helena_ryuu said

      hahaha no problem, I know that at the end of the day, it is firefox with another logo, and it is true, it is in aur, which can be somewhat risky, the fact is that crunchbang is installed on my sister's PC (and for ende iceweasel) and really that the logo is very kawaii ^^ I don't understand why someone can't like it ~ _ ~
      [/ off-topic]

      1.    eliotime3000 said

        Well, I like the Iceweasel logo. At least I am glad in the days in which they have turned me upside down.

  11.   eliotime3000 said

    Sorry for the off-topic, but commenting from Opera Mini is really awkward.

    1.    sieg84 said

      somewhat uncomfortable, but improved with the change in the comments.

  12.   pepper said

    good job

  13.   Emmanuel said

    The tip is more than interesting and useful, but, could it be done in such a way that it runs every time a usb is inserted? 😮
    It would be great to synchronize certain folders on the USB with the system.
    Greetings and excellent contribution.

  14.   Mauricio said

    hahaha, and that you're noob in bash.

    It is one of the reasons why I fell in love with the terminal, because of the way I do some things out there very quickly.

    1.    eliotime3000 said

      And that is true. Also, it is much better than the Windows console.

  15.   Pepe Lopez said

    Excellent tip. I keep it.

  16.   beegoqui said

    Thank you ! … Of great help !

  17.   King said

    Someone has a bash file that I copy from linux the following address /var/log/apt/history.log to the address on windows D: / Copy the file history.log

  18.   King said

    Someone has a bash file that I copy from linux the following address /var/log/apt/history.log to the address in windows D: / Copy the file history.log please send it to my email rey79cm@gmail.com