Deal with a range of file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deal with a range of file names
# 1  
Old 06-08-2013
Deal with a range of file names

Red Hat enterprise Lines 5.8

I have files like below. All the below files are created in the same minute.

Using mv command, I want to move 33 files starting from

HTML Code:
pg424_fcds_1_718519105_34304.dat 
to 
pg424_fcds_1_718519105_34336.dat
to another direcory called /data/fcds_logs/

ls output look like below

Code:
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34300.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34301.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34302.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34303.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34304.dat -------------> start
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34305.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34306.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34307.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34308.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34309.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34310.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34311.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34312.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34313.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34314.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34315.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34316.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34317.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34318.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34319.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34320.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34321.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34322.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34323.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34324.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34325.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34326.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34327.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34328.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34329.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34330.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34331.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34332.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34333.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34334.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34335.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34336.dat -----> end
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34337.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34338.dat
-rw-r--r--. 1 root root    0 Jun  8 09:54 pg424_fcds_1_718519105_34339.dat

Another similair requirement, Can I use rm command to remove the range of files I've mentioned ?
# 2  
Old 06-08-2013
How are determining START and END of the range??
# 3  
Old 06-08-2013
Hi Pik

Using mv command, I want to move 33 files starting from

Code:
 pg424_fcds_1_718519105_34304.dat
 to
 pg424_fcds_1_718519105_34336.dat

The Start and End shown in the ls output is just a comment to identify the range of files we are talking about
# 4  
Old 06-08-2013
You could try this
Code:
mv pg424_fcds_1_718519105_{34304..34336}.dat newdir

or
Code:
for file in pg424_fcds_1_718519105_{34304..34336}.dat 
do
  [ -f "$file" ] && mv "$file" newdir
done

or
Code:
mv pg424_fcds_1_718519105_3430[4-9].dat pg424_fcds_1_718519105_3430[12][0-9].dat pg424_fcds_1_718519105_34303[0-6].dat newdir


Last edited by Scrutinizer; 06-08-2013 at 05:25 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-08-2013
I would choose option two from Scrutinizer's list Smilie
# 6  
Old 06-08-2013
Thank you very much Scrutinizer.
All I need was the first option provided by you . This will save a lot of my repetitive tasks.

Code:
mv pg424_fcds_1_718519105_{34304..34336}.dat newdir

Curly braces {} does the trick. I googled "Curly braces operator + Unix Linux " . Didn't find anything related (or anything useful). Where can I read more about this feauture of curly braces
# 7  
Old 06-08-2013
The curly brace form is a bash sequence expression

From bash man page:
Code:
A sequence expression takes the form {x..y}, where x and y are either integers or single characters.  

When integers are supplied, the expression expands to  each number  between  x  and  y, inclusive.  

When characters are supplied, the expression expands to each character lexicographically between x and y, inclusive.  

Note that both x and y must be of the same type.

For further reference check bash man page and search for "Brace Expansion"
Code:
man bash

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to lookup section of file in a range of another file

In the below, I am trying to lookup $1 and $2 from file1, in a range search using $1 $2 $3 of file2. If the search key from file1 is found in file2, then the word low is printed in the last field of that line in the updated file1. Only the last section of file1 needs to be searched, but I am not... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

3. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

4. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

5. Shell Programming and Scripting

How to deal files with space in names?

I have a bash file as following: #!/bin/sh deal_file(){ printf $1 printf "\t is a file" echo } main(){ for file in `find "$1" ` do deal_file $file done } main $1 then I run ./t.sh .,get the following results: (1 Reply)
Discussion started by: 915086731
1 Replies

6. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

7. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

8. UNIX for Dummies Questions & Answers

How to list file names in a certain date range using ls command?

Hi experts, I Need to print file names in a certain date range using ls:confused:. Please help me with any sample script. Thanks a lot in advance. Regards, Satish (4 Replies)
Discussion started by: satish.vutti
4 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

10. UNIX for Dummies Questions & Answers

Need to print file names in a certain date range using ls

I need to list only file names of a specific date from the command ls -lt. :confused: (2 Replies)
Discussion started by: Shamwari
2 Replies
Login or Register to Ask a Question