- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Moving Top 100 files matching a criteria specified...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 04:42 AM
12-23-2009 04:42 AM
From this folder we need to move 2000 files to two other folders (suppose Folder_A and Folder_B)
here is the criteria,
--> I want to move all files which have 23Dec2009*.log appearing in their name.
--> In a single run of script, i want to move top 1000 files to Folder_A and bottom 1000 files to Folder_B
--> By Top & Bottom i mean the listing that will appear by "ls" command.
Any Help ?
reference thread : ?threadId=1378743
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 05:38 AM
12-23-2009 05:38 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1378743
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 05:44 AM
12-23-2009 05:44 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
Here's a suggestion for you to build upon. Use 'ls -l' to insure that you only look at files. A line that begins with '-' is a regular file and thus a candidate. You can use 'grep' to filter these.
Use 'head' and 'tail' to filter the first and last files and using these subsets, move your files to the appropriate directory. The word "folder" is a Windows term. The correct term in UNIX is "directory".
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 06:17 AM
12-23-2009 06:17 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
I assume you know by now that this is too many files in a directory?
>From this folder we need to move 2000 files
Are there more than 2000 files matching your pattern and some are to be left?
>JRF: Use 'ls -l' to insure that you only look at files.
Note: You only want to do that ll(1) once since you have too many stinkin' files:
ll | awk '/^-.*23Dec2009.*\.log/ { print $9 }' > file_list
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 08:03 AM
12-23-2009 08:03 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
Wouldn't "find -name" be simpler?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 08:12 AM
12-23-2009 08:12 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
> Steven: Wouldn't "find -name" be simpler?
I might agree if we could assume that the source directory has no subordinate directories with files that would match the OP's criteria and that the output is sorted so the OP can snip n-files off the top and bottom.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 05:46 PM
12-23-2009 05:46 PM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
Only if you know how to use -prune and sort.
>JRF: that the output is sorted so the OP can snip n-files off the top and bottom.
The output won't be sorted and you would need to pipe it to sort(1).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 10:01 PM
12-23-2009 10:01 PM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
Once again i tell you that there are thousands of file i.g more than 40k
here are both
1.
ls -ltr /apps/metrica/Spool/gsm_alcatel_bss_b9/load/bad/*18Dec2009*.lif | head -1000 | nawk `{print $9}` | xargs -I -t mv {} /apps/metrica/Spool/gsm_alcatel_bss_b9/load/
2.
mv `ls -1 /apps/metrica/Spool/gsm_alcatel_bss_b9/load/bad/*17Dec2009*.lif | tail -1000` /apps/metrica/Spool/gsm_alcatel_bss_b9/load/
Now what ? :(
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2009 12:15 AM
12-24-2009 12:15 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
We already assumed you would have that problem and my solution would handle the finding of the files.
>1. ls -ltr
If you want them in reverse time order, add that to my solution.
You probably can use just one mv since 1000 is small enough?
mv $(head -1000 file_list) /apps/metrica/Spool/gsm_alcatel_bss_b9/load/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2009 12:45 AM
12-24-2009 12:45 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
what will be the final command that i will run... actually, i am unable to understand and neither i am a good scripter ;-(
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2009 01:28 AM
12-24-2009 01:28 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
How many times will you do this? You still may want a script even if you do it once.
This first step will give you a list of all files for that pattern, sorted in descending order by time, possibly more than 2000:
ll -tr | awk '/^-.*23Dec2009.*\.log/ { print $9 }' > file_list
If you want more REs to add ".lif" (or was that a typo for .log?), you can add with:
/first/ || /second/
Then you need to do the moves. I'm not sure if 1000 is too many for the shell, you'll have to try it:
mv $(head -1000 file_list) FolderA
mv $(tail -1000 file_list) FolderB
Then cleanup: rm -f file_list
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2009 02:43 AM
12-24-2009 02:43 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
grrrrrrrrrrrr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2009 03:49 AM
12-24-2009 03:49 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
The one with awk?
I'm pretty sure I got the ERE correct but I didn't have anything to test. You can toss the ERE and go back to the tried and true RE in grep:
ll -tr | grep '^-.*23Dec2009.*\.log' | awk '{ print $9 }' > file_list
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2009 04:01 AM
12-24-2009 04:01 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
and ll command not accepted.
neither there is bash hospital :(
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2009 04:09 AM
12-24-2009 04:09 AM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
There should be /usr/bin/ll, provided you have /usr/bin in your PATH and you aren't in single user mode. Otherwise you can settle and use "ls -ltr".
>neither there is bash
Why would I want to use bash? :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2009 09:26 AM
12-24-2009 09:26 AM
Solution> ls -ltr /apps/metrica/Spool/gsm_alcatel_bss_b9/load/bad/*18Dec2009*.lif
Getting an "argument list too long" from commands like this is expected. The shell is expanding the filenames before 'ls' can process the expanded argument list.
You can see this behavior by doing:
# cd /path
# echo *
# ls *
# set -f
# echo *
# ls *
# set +f #...the default restored
That said, now rearrange your pipe to snip out the files that match your criteria and then move them. As I originally said, you want to look at only files (not directories). Using the 'awk' Dennis suggested:
# cd /apps/metrica/Spool/gsm_alcatel_bss_b9/load/bad
# ls -l . | awk '/^-.*23Dec2009.*\.lif/ {print $9}' | head -1000 | | xargs -i -t mv {} /apps/metrica/Spool/gsm_alcatel_bss_b9/load
Notice that we used 'awk', not 'nawk' (which is a "new" 'awk' on some UNIX, but not necessary on HP-UX nor probably what you are running on.
We begin by changing into the source directory and doing the 'ls -l .' to search there. This will also mean that 'xargs' will run from this directory.
Notice that the "." in 'awk's regular expression means any character. Hence when we mean ".lif" we escape the dot with a backslash to say that the dot is really a dot only. The caret (^) anchors the "-" to the beginning of the line --- which is what we want to match regular files in the 'ls' output.
In the 'xargs' I changed to the lowercase '-i' for insertion. This is preferred.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2009 07:21 PM
12-24-2009 07:21 PM
Re: Moving Top 100 files matching a criteria specified in 'ls' command to a directory
If you want performance, you don't want to use -i or -l and do the mv(1) one at a time. You just want the default but you need to write a mv_reversed script.
mv_reversed:
#!/usr/bin/ksh
target=$1
shift
mv "$@" $target
Then:
... | xargs -t mv_reversed /apps/metrica/Spool/gsm_alcatel_bss_b9/load
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2009 09:24 PM
12-29-2009 09:24 PM