I'm trying to make a script that will list all directories under a selection as well as the number of files in each.
I cannot get it to work under a symbolic link.
The file structure is:
XXX_20131127_001
dir01 (sym link)
2404x912
file.0000.xxx to
file.0053.xxx
dir02 (sym link)
2404x912
file.0000.xxx to
file.0104.xxx
dir03 (sym link)
2404x912
file.0000.xxx to
file.0457.xxx
dir03 (sym link)
2404x912
I was hoping for an output something like this:
dir01 54 files
dir02 105 files
dir03 458 files
dir04 0 files
On a different note, I also need a script that will display the first and last folders in the directory. Like this:
Hi
Try a recursive combination between foreach and ls to list folders contents and count thier number:
foreach $dir in (ls -d /yourDir)
$nbr=$(ls -lt)
if ($nbr=0)
{echo the dir is empty}
elif ( $(ls -d /yourDir/$in)>0
# a new foreach in the sub dir
foreach()
end
For the second command try the trre command whitch is available in Unix and Unix-like systems:
Sorry I haven't unix to provide an accurate answer
Here is a version where you can specify a list of extensions you want to summarise, but it might play up a bit if you had a directory with mixed file types (eg qt and dpx files), just change EXTS to pipe list of values you would summarise:
EXTS="qt|dpx"
find -L . -depth \( -type l -o -type d \) -print | while read path
do
path=${path#./}
IFS='/' PTH=( $path )
if [[ ${#PTH[@]} -ge ${#prev[@]} ]]
then
indent=""
diff=0
for((i=0;i<${#PTH[@]};i++)) {
if [[ diff -eq 1 || "${PTH}" != "${prev}" ]]
then
echo "$indent${PTH}"
diff=1
fi
indent=" $indent"
}
if ls "$path" | grep -Eq "\.($EXTS)$"
then
echo "$indent$(ls "$path" | grep -E "\.($EXTS)$" | head -1)"
echo "$indent$(ls "$path" | grep -E "\.($EXTS)$" | tail -1)"
else
ls "$path" | sed "s/^/$indent/"
fi
IFS='/' prev=( $path )
fi
done
Every process in unix has it's own distinct working directory, the cd you are using in the above code will not change the working directory of the shell that ran the find command as the exec feature of find will run the cd command in a new sub shell.
You could use -print to output the sub-directory required and then use a cd command from within the shell it's self, something like this:
SUBDIR=$(find . -name "NRV*" -type d -maxdepth 1 -mtime -1 -print | head -1)
[ -n "$SUBDIR" ] && cd "$SUBDIR"
the [-n "$SUBDIR"] part is to test to ensure a directory was found before calling cd, and the head -1 is to select the first directory found when multiple match your find.
This doesn't work.
Oh, I see the problem. What I'm looking for is the newest directory, but I used -mtime 1 ... since it's Monday, the newest file is older than that.
Yep, using mtime -2 it works today, but it won't work tomorrow.
I hadn't thought about files older than a day. Is there a better way to specify the newest file?