0

I have the following files

/folder/abc1.txt.gz
/folder/abc2.txt.gz
/folder/abc3.txt.gz

I would like to make a txt file with the following

abc1 /folder/abc1.txt.gz
abc2 /folder/abc2.txt.gz
abc3 /folder/abc3.txt.gz

I have used the following command

find /folder -name 'abc*.txt.gz' -type f -printf '%f %p\n' > out.txt

This will output:

abc1.txt.gz /folder/abc1.txt.gz
abc2.txt.gz /folder/abc2.txt.gz
abc3.txt.gz /folder/abc3.txt.gz

How can I have only the first part of the filename (without .txt.gz) folowed by the path?

4
  • I'm not sure what you mean by "part of the filename", and "path for all files in the folder". Do you mean that when you find a matching file, you want to print all the other files in the same folder of the file? What if there are multiple matching files at the same folder? Could you provide a practical example of a sample directory/file tree and the exact expected output?
    – aviro
    Dec 15, 2021 at 12:21
  • e.g I have the following files /folder1/abca.vcf.gz /folder1/prrt.vcf.gz /folder1/mefv.vcf.gz and I want a txt that will have abca /folder1/abca.vcf.gz, prrt /folder1/prrt.vcf.gz, mefv /folder1/mefv.vcf.gz one line for each file... hope this is more clear... Dec 15, 2021 at 13:23
  • First, please edit your question and add the relevant details. Second, do you mean that you want to see the filename without the extension? If that's what you mean, you also need to specify that in the question. The question should be clear so that anyone that reads it understand your exact intention, without having to go through the comments for clarifications.
    – aviro
    Dec 15, 2021 at 13:30
  • edited original to make it more clear.. Dec 15, 2021 at 14:05

4 Answers 4

0

If it's always the same extension, you could remove it this way:

find /folder -name 'abc*.txt.gz' -type f -printf '%f %p\n' |sed 's/\.txt.gz//'

This sed command will only remove the first occurrence of .txt.gz.

If you want to remove any extension:

find /folder -name 'abc*.txt.gz' -type f -printf '%f %p\n' |sed 's/\.[^[:space:]]* / /'

Assuming that there aren't any spaces in the extension itself, it will remove all the non-space characters after the first dot . until the first space.

3
  • Thank you very much. This is very helpful! Dec 15, 2021 at 14:35
  • @christoforosgiatzakis If the answer helped you, you do not need to comment. It's usually expected to upvote the answer and accept it (press on the "V" sign below the votes of the answer)
    – aviro
    Dec 15, 2021 at 14:47
  • Note that a file called abc-01.txt-gzillion.txt.gz would be listed as abc-01illion.txt.gz.
    – Kusalananda
    Dec 15, 2021 at 18:19
0

Since you're seeking a solution using basename, and if we can assume that the .txt.gz suffix is constant across your set of files, and that you're using bash:

declare -a files

readarray -t files < <( find /folder -name '*.txt.gz' -type f )

for f in "${files[@]}"
do
        printf '%s %s\n' "$(basename "$f" '.txt.gz')" "$f"
done

$ test.sh
abc1 /folder/abc1.txt.gz
abc2 /folder/abc2.txt.gz
abc3 /folder/abc3.txt.gz
0

For shell bash:

find folder/ -name 'abc*.txt.gz' -exec bash -c ': ${0##*/}; echo ${_%%.*} $0' {} \;

: an empty command, but its argument ${0##*/} is assigned to the $_ internal variable (the last argument to the previous simple command).

1
  • Since you aren't quoting your variables in that in-line script, a file called abc * .txt.gz may cause issues.
    – Kusalananda
    Dec 15, 2021 at 18:23
0

Just use basename to get rid of the directory path and the suffix from your files, then use -print to print the full pathname, and then combine the two with paste:

find /folder -name 'abc*.txt.gz' -type f -exec basename {} .txt.gz \; -print |
paste - -

This would create lines of output that have two tab-delimited columns, where the first column would be the output of basename and the second would be the full pathname of the found file. Would you want a single space as delimiter instead, then use paste -d ' ' - - in place of paste - -.

This relies on no name containing embedded newlines, and names containing the delimiter used by paste may also result in a list that is difficult to parse.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .