Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
I have a lot of files. All of these files are in numerical order. They go from 1.txt to 1000.txt So, as you could figure, there is 165.txt and 954.txt ...
  1. #1
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261

    Ok, I'm wondering if BASH can do this:

    I have a lot of files. All of these files are in numerical order. They go from

    1.txt to 1000.txt
    So, as you could figure, there is 165.txt and 954.txt somewhere in the middle.
    I simply have 1000 files and all are named accordingly with a numerical position.

    I want to create a script. This script grabs files 165.txt through 953.txt and put them in folder labeled texts1.

    How do I create a script that does that?

    For what I know, I would have to create a script listing all files I want to collect and move. To create a script like that would be very, very time consuming. Is there a way to say, "Grab files 165 through 953, and move them to this folder"? Is there a way to do that with perhaps 1 or 2 lines of code? Three? Something under 20?

    I eventually want to say, "Ok, now grab 1 through 164 and put them here. Good, now put 954 through 1000 and put them here."
    Afterwards, I might go to a different folder and choose a large collection of different, numerically ordered files to place in different folders.

    How do I create a script to do what I want?
    I'm totally new to bash (BASH?) and its ways.
    I'm more familiar with DOS, because I grew up with it since the 90s.
    I've read bash can do things Windows and DOS can't do.
    Can bash do what I want?

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Code:
    mv $(ls *.txt|sort -n|sed -n '165,953p') texts1
    Regards

  3. #3
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261
    Quote Originally Posted by Franklin52 View Post
    Code:
    mv $(ls *.txt|sort|sed -n '165,953p') texts1
    Alright, cool. Thank you very much. I'm wondering how you figured this out and knew it. Is there a place I could learn how to build a command like that?

    Also, why did you put a p after the 953 in "953p"? Does it create some kind of end mark?

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Sorry, this line:

    Code:
    mv $(ls *.txt|sort|sed -n '165,953p') texts1
    must be:

    Code:
    mv $(ls *.txt|sort -n|sed -n '165,953p') texts1
    I forgot to note that I've edit that.
    Next I'll explain the code.

    Regards

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Code:
    ls *.txt --> list all files that match *.txt
    
    sort -n --> nummeric sort
    
    sed -n --> print only files that match the pattern
    
    165,953p --> print lines 1 - 953
    Regards

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    The best resource for learning how to do stuff like this is:
    http://www.tldp.org/LDP/abs/html/

    Also, as you start using these, you'll learn how powerful some commands can be. sed can do amazing things, and awk is essentially its own programming language.
    DISTRO=Arch
    Registered Linux User #388732

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Agent-X View Post
    I'm totally new to bash (BASH?) and its ways.
    since you are totally new, then let's go step by step
    Code:
    for ((num=165; num <= 953 ; num++))  
    do
      if [ -f "$num.txt" ];then
        mv $num.txt $somewhere
      fi
    done

  8. #8
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261
    Hmm, I've been reading about sed. To be honest, I gave .txt files for examples. I'm not really editing text files at all. I'm using a different type of extension. Imagine if it were .avi.

    Does that make much of a difference?
    After reading about sed, it looks like some type of text editor, which could cause problems because I'm not editing text files. I simple said text files as an example file type.

  9. #9
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Agent-X View Post
    Hmm, I've been reading about sed. To be honest, I gave .txt files for examples. I'm not really editing text files at all. I'm using a different type of extension. Imagine if it were .avi.

    Does that make much of a difference?
    After reading about sed, it looks like some type of text editor, which could cause problems because I'm not editing text files. I simple said text files as an example file type.
    sed is a stream editor. It doesn't "edit" your file while its going through it. only when you tell it to, like using > to redirect to a file, or using -i option (in place ) in gnu sed, then sed will change your file. learn what the bash shell built-in commands can do for you, before learning tools like sed/awk/grep/sort/paste/cut etc etc...

  10. #10
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Quote Originally Posted by Agent-X
    I eventually want to say, "Ok, now grab 1 through 164 and put them here. Good, now put 954 through 1000 and put them here."
    Afterwards, I might go to a different folder and choose a large collection of different, numerically ordered files to place in different folders.
    Code:
    #!/bin/bash
    
    for num in `seq $1 $2`  
    do
      if [ -f "$num.txt" ];then
        mv $num.txt $3
      fi
    done
    echo "its done"
    save this script as 'mv_files.sh' or whatever name you want to assign.

    * move files 45 - 170 to folder new_files.

    execute like this
    Code:
    sh mv_files.sh 45 170 new_files
    assign any sequence, any folder name. pass correct Path of folder.
    replace .txt in script with any other extension.

    HTH,
    Casper
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

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
  •  
...