Showing posts with label code snippets unix scripting. Show all posts
Showing posts with label code snippets unix scripting. Show all posts

Wednesday, 20 January 2010

Listing today's files in a directory without using find

Had an issue today where I needed to get the today's log files from a Unix directory but couldn't use find. Normally I could use the time function in find to do this as follows where . is the directory to search from (. is the current directory):
find . -mtime +0
However the only way I could get the date for the files was by using ls -al and then removing all the extra data to leave just the filename.

The following script takes an argument of the directory you want to list all the files with the current date:

#!/bin/sh
die () {
echo >&2 "$@"
exit 1
}

[ "$#" -eq 1 ] || die "target directory argument required, $# provided"

target=$1
myvar=`date "+%h %d"`

for file in `ls -al $target | grep "$myvar" | sed 's/[-drwx]* *[0-9] *[a-zA-Z0-9]* *[a-zA-Z0-9]* *[0-9]* *[a-zA-Z]* [0-9]* *[0-9]*:[0-9]* *\.*//'`; do
echo $file
done


Wednesday, 1 April 2009

Looping through a file search in unix

Little handy script in unix to get file details from a find result
#!/bin/sh

# Find all the log files and loop through them
for file in `find $HOME -type f -name \*.log`; do
  # Get the file details
  ls -al $file
done