Bash command to cut file name off of path? (SOLVED)
Bash command to cut file name off of path? (SOLVED)
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.
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.
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), tryand
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
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##*/}
Code: Select all
DIRNAME=${PATHNAME%/*}
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
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:sunburnt wrote:tried them out & they worked great, I'm going to modify to get the file extention, 1 & 2 dot if I can.
Code: Select all
EXTENSION=${PATHNAME##*.}
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
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.
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.
Why create a subshell and execute a command... there is a Bash variable $PWD .MU wrote:1. myDir=`pwd`
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 ?2. probepart (does not list all drives, for example not the ones ony external USB-drive)
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.3. stat thefile
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
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.
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.