Find Files and Print Only Their Parent Directories

Find files and print only their parent directories

Am I missing something here. Surely all this regex and/or looping is not necessary, a one-liner will do the job. Also "for foo in $()" solutions will fail when there are spaces in the path names.

Just use dirname twice with xargs, to get parent's parent...

# make test case
mkdir -p /nfs/office/hht/info
mkdir -p /nfs/office/wee1/info
touch /nfs/office/hht/info/.user.log
touch /nfs/office/wee1/info/.user.log

# parent's parent approach
cd /nfs//office/ && find . -name '.user.log' | xargs -I{} dirname {} | xargs -I{} dirname {}

# alternative, have find print parent directory, so dirname only needed once...
cd /nfs//office/ && find . -name ".user.log" -printf "%h\n" | xargs -I{} dirname {}

Produces

./hht
./wee1

Find files and print only partial-parent-folder+filename

Use find -printf with %P to avoid including base_folder in the output:

find "$base_folder" -type f -mmin +2 -name "*.csv" -printf '%P\n'

How do I print all the parent directories in Bash

File:

$ cat subdirs
./dir1/dir2/dir3
./dir4/dir5

Script (ugly a bit, but works fine):

#!/bin/bash

while read line
do
counter=$(echo "${line}" | grep -o '/' | wc -l)
i=1
j=$(($i + 1))
echo "Original string: ${line}"
echo "Its parent directories:"
while [ "${counter}" -gt 0 ]
do
echo "${line}" | cut -d'/' -f$i-$j
counter=$(($counter - 1))
j=$(($j + 1))
done
echo "Next one if exists..."
echo ""
done < subdirs

Output:

Original string: ./dir1/dir2/dir3
Its parent directories:
./dir1
./dir1/dir2
./dir1/dir2/dir3
Next one if exists...

Original string: ./dir4/dir5
Its parent directories:
./dir4
./dir4/dir5
Next one if exists...

How to find all files with a particular parent directory in linux?

Use -path switch with find

find . -path \*/foldername/filename.extension

How to use 'find' to return parent directory

one way of many:

find /   -name 'myfile' -type f -exec dirname {} \;

How to list only files and not directories of a directory Bash?

Using find:

find . -maxdepth 1 -type f

Using the -maxdepth 1 option ensures that you only look in the current directory (or, if you replace the . with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.

Sort find by parent directories before subdirectories

You can sort by the number of slashes.

find Temp/ -name '*.txt' \
| perl -pe 'print tr{/}{}, "\t"' \
| LC_ALL=C sort -k1,1n -k2 \
| cut -f2-

The tr operator returns the number of matches, i.e. the number of slashes in the string in Perl.

The sort then sorts the lines numercially and cut removes the number from each line.

How to get just the parent directory name of a specific file

Use File's getParentFile() method and String.lastIndexOf() to retrieve just the immediate parent directory.

Mark's comment is a better solution thanlastIndexOf():

file.getParentFile().getName();

These solutions only works if the file has a parent file (e.g., created via one of the file constructors taking a parent File). When getParentFile() is null you'll need to resort to using lastIndexOf, or use something like Apache Commons' FileNameUtils.getFullPath():

FilenameUtils.getFullPathNoEndSeparator(file.getAbsolutePath());
=> C:/aaa/bbb/ccc/ddd

There are several variants to retain/drop the prefix and trailing separator. You can either use the same FilenameUtils class to grab the name from the result, use lastIndexOf, etc.

list only folders (ls -d) relative to parent in bash

Sounds like you're looking for find, along with the -type d switch to limit the results to directories:

find .. -type d

You can use .. to start from the parent directory.

Depending on your version of find, you may be able to specify -mindepth and -maxdepth to limit the results:

find .. -mindepth 1 -maxdepth 2 -type d


Related Topics



Leave a reply



Submit