0

I want to find a particular filename from a particular directory.

Also I don't know the file name on that particular directory. I only know the extension of that file i.e. ".uploaded". So using that .uploaded extension i want to find the filemane.

I want the file name in output without full path up to the particular directory, I just want filename.

So how can i do?

  • Please include an actual example. It is really difficult to understand what you want at the moment, but an example is usually sufficient to explain such a task. – Daniel Andersson Apr 29 '14 at 8:24
0

You can remove the path to the file by rev-cut-rev:

ls dir/*.uploaded | rev | cut -d/ -f1 | rev
| improve this answer | |
0

Sample directory structure:

$ ls -1 test
a
b.uploaded
c.uploaded
d.uploaded
e

With find, suitable for piping through to another command via xargs:

$ EXT='.uploaded'; find dir -maxdepth 1 -name "*$EXT" -exec basename {} "$EXT" \;
d
b
c

In shell script, using basename to strip directory and suffix:

$ EXT=".uploaded"; for i in dir/*"$EXT"; do basename -- "$i" "$EXT"; done
b
c
d

In shell script, using parameter expansion:

$ EXT=".uploaded"; for i in dir/*"$EXT"; do i=${i##*/}; printf '%s\n' "${i%$EXT}"; done
b
c
d

Note that you might hit several files. You have not defined what you want to happen after the file. If you are sure that there is only one file that will match, you can use basename directly:

$ rm dir/{b,d}.uploaded
$ basename dir/*.uploaded .uploaded
c

but this will fail if the glob matches multiple files.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.