-1

I need to write a script that lists all the files with a .gif extension in the current directory and all its sub-directories BUT DO NOT use ANY of:

  • basename
  • grep
  • egrep
  • fgrep
  • rgrep
  • &&
  • ||
  • ;
  • sed
  • awk

AND still include hidden files.

I tried find . -type f -name '*.gif' -printf '%f\n' which will succesfully display .gif files, but still shows extension. Here's the catch: if I try to use cut -d . -f 1 to remove file extension, I also remove hidden files (which I don't want to) because their names start with ".".

Then I turned to use tr -d '.gif' but some of the files have a 'g' or a '.' in their name.

I also tried to use some of these answers BUT all of them include either basename, sed, awk or use some ";" in their script.

With so many restrictions I really don't know if it's even possible to achieve that but I'm supposed to.

How would you do it?

Darvid
  • 15
  • 2

3 Answers3

1

files/dirs structure:

$ tree -a
.
├── bar
├── bar.gif
├── base
│   └── foo.gif
├── foo
│   └── aaa.gif
└── .qux.gif

3 directories, 4 files

Code

find -type f -name '*.gif' -exec bash -c 'printf "%s\n" "${@%.gif}"' bash {} +

Output

./bar
./.qux
./foo/aaa
./base/foo

Explanations

Parameter Expansion expands parameters: $foo, $1. You can use it to perform string or array operations: "${file%.mp3}", "${0##*/}", "${files[@]: -4}". They should always be quoted. See: http://mywiki.wooledge.org/BashFAQ/073 and "Parameter Expansion" in man bash. Also see http://wiki.bash-hackers.org/syntax/pe.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

Something like:

find . -name '*.gif' -type f -execdir bash -c 'printf "%s\n" "${@%.*}"' bash {} +
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
0

Using :

perl -MFile::Find::Rule -E '
    say s/\.gif$//r for File::Find::Rule
        ->file()
        ->name(qr/\.gif\z/)
        ->in(".")
'

Output:

bar
.qux
foo/aaa
base/foo
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223