Bash command to cut file name off of path? (SOLVED)

Using applications, configuring, problems
Post Reply
Message
Author
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

Bash command to cut file name off of path? (SOLVED)

#1 Post by sunburnt »

I don't know why I can't get a grasp of Bash scripting.
I have the command to get the file name cut from the path:
S=/1/2/3
F=`echo $S | sed 's#^.*\/##'`
$F = 3
I can't seem to figure out the command to get the path cut from the file name.
I tried: P=`echo $S | sed 's#\/.*$##'` $P = nothing.
I tried: P=`echo $S | sed 's#\$F$##'` $P = /1/2/3.
I tried: P=`echo $S | sed 's#\$F##'` $P = /1/2/3.
I tried: P=`echo $S | sed 's#"$F"##'` $P = /1/2/3.

It'd be nice to use the variable ($F) as the sed search value, allows a much more flexable usage.
Last edited by sunburnt on Tue 14 Feb 2006, 08:02, edited 1 time in total.

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#2 Post by GuestToo »


User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#3 Post by sunburnt »

Mucho thanks GuestToo, I need to find a catagorized Bash book somewhere, so many commands to know.

User avatar
jmarsden
Posts: 265
Joined: Sat 31 Dec 2005, 22:18
Location: California, USA

#4 Post by jmarsden »

Note that commands such as sed, basename and dirname are not Bash commands, really. They are external Unix/Linux commands that can be called from any shell, or indeed from any other running process.

If you need a way to get the file from a pathname stored in a variable PATHNAME in Bash itself (without using external commands), try

Code: Select all

FILENAME=${PATHNAME##*/}
and

Code: Select all

DIRNAME=${PATHNAME%/*}
These work in memory, no disk access to external utility programs needed, but they are Bash-specific, and not guaranteed to work in other shells.

To learn Bash scripting, I suggest the Bash Reference Manual as a reference, and the Advanced Bash Scripting Guide. To learn the rest of Unix and Linux... is another matter! For Linux, one decent general source is Rute User's Tutorial and Exposition. Another is the printed book Running Linux, now in its 5th edition.

The best-known reading list for Linux people is of course the Linux Reading List HOWTO by Eric Raymond.

Jonathan

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#5 Post by sunburnt »

jmarsden; tried them out & they worked great, I'm going to modify to get the file extention, 1 & 2 dot if I can.
Many thanks...

Guest

#6 Post by Guest »

sunburnt wrote:tried them out & they worked great, I'm going to modify to get the file extention, 1 & 2 dot if I can.
In general in Linux it is better to determine the type of a file using the file command than using extensions. I wrote something in another thread here in the forums about that. Getting the "last" file extension is trivial if you know one exists:

Code: Select all

EXTENSION=${PATHNAME##*.}
But what if there isn't an extension? What if the the initial filename is fred-0.24 for example? Is it sane to say that it has a "file extension" of 24 ? This gets even more difficult for "two dot" extensions, considering fred.tar.gz is OK, but then try fred-0.24.gz and puppy-1.0.7alpha.zip as well :-)

I'd suggest either testing for .gz and .bz2 explicitly and then rechecking for some small set of common extensions behind them, or just not worrying about file extensions at all, just use the file command instead.

Jonathan

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#7 Post by sunburnt »

Guest; exactly the issues I've been dealing with, & the same conclusions that I used in my code.
Linux leaves a number of things without any solution, I have a number of unanswered Qs.

1. Anyway to get the current dirs. path?
2. How to find & identify the type of all the local drives?
3. File access logging; path & file name, access time, etc.

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#8 Post by MU »

1.
myDir=`pwd`

2.
probepart (does not list all drives, for example not the ones ony external USB-drive)

3.
stat thefile
for example last access
stat -c %x /root/.jwmrc

more info:
stat --help

Mark

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#9 Post by sunburnt »

Man MU, your a veritable fountain of info, hang on I'm making my next list...

User avatar
jmarsden
Posts: 265
Joined: Sat 31 Dec 2005, 22:18
Location: California, USA

#10 Post by jmarsden »

MU wrote:1. myDir=`pwd`
Why create a subshell and execute a command... there is a Bash variable $PWD .
2. probepart (does not list all drives, for example not the ones ony external USB-drive)
Interesting! Is this distribution-specific? I'd have said that, under Linux 2.x kernels, you can just read /proc/partitions to get the partition list and then, if necessary, use fdisk -l to see more details including fs type. Why doesn't probepart in Puppy respond more usefully to command line options such as -? or --help or -v or --version ?
3. stat thefile
ls can also display creation/modification/access times. Whether it is more useful to use stat or ls depends on what exactly you'll be doing with the information you retrieve.

Keeping to my "stay in Bash" theme a bit more, you can do modification time comparisons between files with [ file1 -nt file2 ] and [ file1 -ot file2 ] (where -nt is "newer than" and -ot is "older than". Combined with touch, this can be very handy. Ofen you don't really care exactly when a file was created/accessed/modified, just whether that operation happened more or less than N days ago, or before or after some other file or directory was changed.

Overall, I think sunburnt's statement that "Linux leaves a number of things without any solution" need challenging pretty vigorously -- we seem to be finding multiple solutions to all his questions, so far :-)

Jonathan

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#11 Post by sunburnt »

My main interest in knowing what files are access is to find out which ones are being used frequently.
These files I'll then put into the LanPuppy client PC's image.gz file, so they'll be run from the ramdisk.
This speeds the clients up & reduces the LAN load because the client's usr_cram file is Samba mounted.
Accordingly, I need to move low access files out of the image.gz file, to reduce it's size to free memory.
Anything not used at boot time doesn't have to be in the image.gz file, that could save a lot of space.

Post Reply