bash - How to create a txt file with a list of directory names if directories have a certain file - TagMerge
5How to create a txt file with a list of directory names if directories have a certain fileHow to create a txt file with a list of directory names if directories have a certain file

How to create a txt file with a list of directory names if directories have a certain file

Asked 10 months ago
2
5 answers

Let's assume that any foo/y/z is a file (that is, you do not have directories with such names). If you had a really large number of such files, storing all paths in a bash variable could lead to memory issues, and would advocate for another solution, but about 800 paths is not large. So, something like this should be OK:

declare -a names=(*/y/z)
printf '%s\n' "${names[@]%%/*}" > IDlist.txt

Explanation: the paths of all z files are first stored in array names, thanks to a glob pattern: */y/z. Then, a pattern substitution is applied to each array element to suppress the /y/z part: "${names[@]%%/*}". The result is printed, one name per line: printf '%s\n'.

If you also had directories named z, or if you had millions of files, find could be used, instead, with a bit of awk to retain only the leading directory name:

find . -mindepth 3 -maxdepth 3 -path './*/y/z' -type f |
  awk -F/ '{print $2}' > IDlist.txt

If you prefer sed for the post-processing:

find . -mindepth 3 -maxdepth 3 -path './*/y/z' -type f |
  sed 's|^\./\(.*\)/y/z|\1|' > IDlist.txt

These two are probably also more efficient (faster).

Note: your initial attempt could also work, even if using bash loops is far less efficient, but it needs several changes:

#!/bin/bash

for d in *; do
    if [ -d "$d/y" ]; then
        for f in "$d"/y/*; do
            if [ "$f" = "$d/y/z" ]; then
                printf '%s\n' "$d" >> IDlist.txt
            fi
        done
    fi
done

As noted by @LéaGris, printf is better than echo because if d is the -e string, for instance, echo "$d" interprets it as an option of the echo command and does not print it.

But a simpler and more efficient version (even if not as efficient as the first proposal or the find-based ones) would be:

#!/bin/bash

for d in *; do
    if [ -f "$d/y/z" ]; then
        printf '%s\n' "$d" 
    fi
done > IDlist.txt

As you can see there is another improvement (also suggested by @LéaGris), which consists in redirecting the output of the entire loop to the IDlist.txt file. This will open and close the file only once, instead of once per iteration.

Source: link

1

You can first try to do some filtering using find

Below will list all z files recursively within current directory

Then let's say the one of the output was

./dir001/y/z

Then you can extract required part using multiple ways grep, sed, awk, etc

e.g. with grep

find . -type f | grep z | grep -E -o "y.*$"

will give

y/z

Source: link

1

This should solve it:

for f in */y/z; do
    [ -f "$f" ] && echo ${f%%/*}
done

Note: If there is a possibility of weird top level directory name like "-e", use printf instead of echo, as in the comment below.

Source: link

0

Reading and writing data to files using Python is pretty straightforward. To do this, you must first open files in the appropriate mode. Here’s an example of how to use Python’s “with open(…) as …” pattern to open a text file and read its contents:
with open('data.txt', 'r') as f:
    data = f.read()
open() takes a filename and a mode as its arguments. r opens the file in read only mode. To write data to a file, pass in w as an argument instead:
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
Suppose your current working directory has a subdirectory called my_directory that has the following contents:
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
>>>
>>> import os
>>> entries = os.listdir('my_directory/')
>>>
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']

Source: link

0

Searching on windows the “cmd” name an open as administratorNavigate to your path where you need to list the file by type cd and the path:
cd c:\Test\
Click EnterExecute the following command
dir
If you want to list the files in all the subfolders as well as the main folder, enter:
dir /s
The dir command can also be used to search for specific files and directories by using wildcards. For example, to list files or directories that begin with the letter “B” you could type:
dir b*
Open the command line in the folder of interest. Example:
cd c:\Test\

Source: link

Recent Questions on bash

    Programming Languages