Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Bash Script: How to get file without path?

Tags:

linux

bash

path

ls

I am trying to write a very very simple script in Linux.
Let me show you the code first:

#!/bin/bash
# The shell program uses glob constructs and ls
# to list all entries in testfiles, that have 2
# or more dots "." in their name.

ls -l /path/to/file/*.*.*

When I run this code with bash myscript command, I get something like: /path/to/file/file.with.three.dots

But I don't want this. I want to show only the file name, not the path.
Then I tried:

ls -l *.*.*

But this time is shows me the files, only if I am inside the /path/to/file/.
How can I set the path, so when running the script from any place, it will output the name of the files in the /path/to/file/?

Thank you!

like image 813
m.spyratos Avatar asked Dec 13 '25 13:12

m.spyratos


1 Answers

basename path/to/file.b.c should give you file.b.c

However re-reading the question, I think a temporary cd to the path and then an ls may be better:

(cd /path/to/file; ls -l *.*.*)
like image 133
John3136 Avatar answered Dec 15 '25 04:12

John3136



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!