Regarding handling path/filenames with possible embedded spaces:
An awk idiom for removing just one or a few fields and printing "the rest of a line" (without an explicit for loop) is to simply set those fields to an empty string and print $0.
So you can do something like:
To effectively just remove the first "word" from each line. (BTW: this example would still reflect the space before $2 --- so you'd want to use BEGIN { OFS="" } if you wanted to avoid the problem.
Ok... Found a bug soon as I posted this. In the previous example the "awk" will only print out the string after the number up to the end of the line or another space. Not good if you have files or directories with spaces. This would work better.
just found out this bug and wanted to tell you... now i saw you corrected yourself
just found out this bug and wanted to tell you... now i saw you corrected yourself
big thanks for this one!
Thanks to both of you, this was a huge help for me.
I wanted to add this:
By adding "-maxdepth 1" to the find command, it prevents going through subdirectories, which I didn't want.
Of more use to others though, by using "head -n -3", I can get a list of all but the three newest files, and the "-r" flag on xargs prevents it from running "rm -v" if there are three or fewer files.
However, right now, xargs won't accept files with spaces... There are a number of ways around that, you could use "sed s/ /\\ /g" to replace all spaces with "\ ", which would work... I didn't need it for my purposes, so I left it out.
Anyway, thanks a bunch, I've been on and off wondering how to do this for a while, and you put me on the right path.
Hi
I am unable to find files, those are present anywhere in the same directory tree, based on the creation date. I need to find the files with their path, as I need to create them in another location and move them. I need some help with a script that may do the job.
Please help (2 Replies)
Hi,
My apologies if my query is already available on this forum but I am new and could not find.
I need a script to list all directories/sub directories and files with permissions/groups/owners. The script would run from home directory and should capture every directory. How do I do this?
... (4 Replies)
I'm using a directory naming convention to organize files as exemplified here:
2012/Aug/week-20-Aug/23-Thu/tuv.txt
2012/Aug/week-27-Aug/30-Thu/abc.txt
2012/Sep/week-27-Aug/01-Sat/def.txt
2012/Sep/week-03-Sep/07-Fri/xyz.txt
How do I write a command that will list the file names abc.txt and... (4 Replies)
Hey! I have found similar posts both here and on other sites regarding this, but I cannot seem to get my script to work. I want to delete the oldest file in a test directory if there are more than two files. My script is currently:
#!/bin/bash
MEPATH=/usr/local/bin/test
FILECOUNT=`ls... (4 Replies)
I am trying to determine the oldest and most recent files in a huge directory. I am using an ls -tr statement outside my find statement. The directory is too big and I am getting an "arg list too long" error. Is there something I can put in my find statement that doesn't create a list to... (2 Replies)
Hi all,
I am a newbie to scripting and I need your help regarding finding the oldest file in a particular directory. My intention is to remove that oldest file.
Are there any options available with the "find" command to do this..
Thanks in advance for your help
Pavan (4 Replies)
I am using a bash script to perform some automated maintenance on files in a directory. When I run the script using $sh -x script.sh <directory> the script works fine. It sets the variable to the oldest file, and continues on. However when I run the script like this $./script.sh <directory>, it... (5 Replies)
Hi all,
I need your assistance in removing the oldest file in a directory.
I posted the same thread 3 days back and I got the following answer
ls -1 -t | tail -1 | xargs rm
which is not covering the case when there are directories older than the oldest file.
So, could you please... (2 Replies)
I'm writing a script to find the oldest file in a directory. I know this can be done by using ls -rt | tail -1 but these are rather large directories and that can be somewhat slow since the script will be running constantly.
Are there any other ways to do this that would be faster? I looked to... (2 Replies)