Ubuntu bash script: how to split path by last slash?

bash split directory path into array
bash split string
bash get filename from path
bash basename
split function in shell script
how to get the file name from the path in bash
shell script remove filename from path
bash remove extension

I have a file (say called list.txt) that contains relative paths to files, one path per line, i.e. something like this:

foo/bar/file1
foo/bar/baz/file2
goo/file3

I need to write a bash script that processes one path at a time, splits it at the last slash and then launches another process feeding it the two pieces of the path as arguments. So far I have only the looping part:

for p in `cat list.txt`
do
   # split $p like "foo/bar/file1" into "foo/bar/" as part1 and "file1" as part2
   inner_process.sh $part1 $part2
done

How do I split? Will this work in the degenerate case when path has no slashes?

Thx


Use basename and dirname, that's all you need.

part1=`dirname "$p"`
part2=`basename "$p"`

Split a /path/to/file, In a standard shell: $ path=/aaa/bbb/ccc/ddd/eee/fff.txt $ tail="${path#/*/*/}" $ head="${path%/$tail}" $ echo "$head" "$tail" /aaa/bbb  Unix / Linux Shell: Get Third Field Separated by Forward Slash (/) Delimiter last updated April 29, 2012 in Categories BASH Shell I ‘m writing a shell script for automation purpose.


A proper 100% bash way and which is safe regarding filenames that have spaces or funny symbols (provided inner_process.sh handles them correctly, but that's another story):

while read -r p; do
    [[ "$p" == */* ]] || p="./$p"
    inner_process.sh "${p%/*}" "${p##*/}"
done < list.txt

and it doesn't fork dirname and basename (in subshells) for each file.

The line [[ "$p" == */* ]] || p="./$p" is here just in case $p doesn't contain any slash, then it prepends ./ to it.

See the Shell Parameter Expansion section in the Bash Reference Manual for more info on the % and ## symbols.

bash - How to display the paths in $PATH separately, If you only want to replace the first : , remove second slash: echo -e Here, the script is splitting its input on : and then loops over each split  If your last field is a single character, you could do this: Check string manipulation in bash. This doesn't work: it gives the last character of a, not the last field. True, that's the idea, if you know the length of the last field it's good.


Great solution from this source

p=/foo/bar/file1
path=$( echo ${p%/*} )
file=$( echo ${p##/*/} )

This also works with spaces in the path!

basename, basename is a standard computer program on Unix and Unix-like operating systems. When basename is given a pathname, it will delete any prefix up to the last slash ( '/' ) character and return the result. basename is described in the Single UNIX Specification and is primarily used in shell scripts. string. A pathname. suffix. If specified, basename will also delete the suffix. If filename does not contain a slash, filenames in PATH are used to find the directory containing filename. The file searched for in PATH need not be executable. When bash is not in posix mode , the current directory is searched if no file is found in PATH .


Here is one example to find and replace file extensions to xml.

for files in $(ls); do

    filelist=$(echo $files |cut -f 1 -d ".");
    mv $files $filelist.xml;
done

File::Basename, NAME. File::Basename - Parse file paths into directory, filename and suffix. This function is provided for compatibility with the Unix shell command Also unlike fileparse() , dirname() does not include a trailing slash on its returned path. I'm trying to remove part of the path in a string. I want to remove the first part /path/to/file/drive and produce the output: Note: I have several paths in a while loop, with the same /path/to/file/drive in all of them, but I'm just looking for the 'how to' on removing the desired string. I found some examples, but I can't get them to work:


Python How To Get The Last Directory Name In A Path, split: Return tuple (head, tail) where tail is everything after the final slash. print os.​path.split(path) #('/test1/test2/test3', 'test.txt') print os.path  Split string by delimiter and get N-th element. Asked 3 years, 4 months ago. Active 1 year, 4 months ago. Viewed 336k times. I have a string: one_two_three_four_five. I need to save in a variable A value two and in variable B value four from the above string. shell string cut split. improve this question. edited Mar 21 '18 at 9:03. 3 bronze badges.


Extract filename from file using shell script, If you are in a Linux-like environment you can use the basename utility: If instead all you know is it is the last part of a line but don't know the directory structure, you can use foo=/path/to/file/split/by/slashes.txt $ echo ${foo##*/} slashes.txt. Hi all, I need to know way of inserting backward slash before forward slash. My problem is that i need to supply directory path as an argument while invoking cshell script. This argument is further used in script (i.e. sed is used to insert this path in some file). So i need to place \ in front (2 Replies)


Chapter 9 – Reading and Writing Files, The part of the filename after the last period is called the file's extension and tells you a Backslash on Windows and Forward Slash on macOS and Linux So while Path(r'spam\eggs') refers to two separate folders (or a file eggs in a folder  Hi all, I need to know way of inserting backward slash before forward slash. My problem is that i need to supply directory path as an argument while invoking cshell script. This argument is further used in script (i.e. sed is used to insert this path in some file). So i need to place \ in front (2 Replies)