How to remove end folder name from a path in Linux script?
You can use the dirname
command to do what you're asking for, it remove the last "part" from a file. If what you give it is a directory, you'll get the parent directory.
parent=$(dirname /your/path/here)
But doing a cd..
with a script is not possible - the cd
would only affect the shell that the script is running in, not the shell that invoked the script.
So you have to use an alias or a function.
alias cd..='cd ..'
Or
cdp() {
cd ..
}
How to remove last directory from a path with sed?
sed 's,/*[^/]\+/*$,,'
If it's part of the shell script, then dirname
will be definitely more clear.
How to remove first and last folder in 'find' result output?
You could use parameter expansion:
d=path/to/my/dir
d="${d#*/}" # remove the first dir
d="${d%/*}" # remove the last dir
echo $d # "to/my"
Remove part of path on Unix
You can also use POSIX shell variable expansion to do this.
path=/path/to/file/drive/file/path/
echo ${path#/path/to/file/drive/}
The #..
part strips off a leading matching string when the variable is expanded; this is especially useful if your strings are already in shell variables, like if you're using a for
loop. You can strip matching strings (e.g., an extension) from the end of a variable also, using %...
. See the bash
man page for the gory details.
How do I remove the file suffix and path portion from a path string in Bash?
Here's how to do it with the # and % operators in Bash.
$ x="/foo/fizzbuzz.bar"
$ y=${x%.bar}
$ echo ${y##*/}
fizzbuzz
${x%.bar}
could also be ${x%.*}
to remove everything after a dot or ${x%%.*}
to remove everything after the first dot.
Example:
$ x="/foo/fizzbuzz.bar.quux"
$ y=${x%.*}
$ echo $y
/foo/fizzbuzz.bar
$ y=${x%%.*}
$ echo $y
/foo/fizzbuzz
Documentation can be found in the Bash manual. Look for ${parameter%word}
and ${parameter%%word}
trailing portion matching section.
how to delete a sub directory whose name is appended to the end of its parent folder's name
Loop over the directories, use parameter expansion to remove everything before the last underscore:
#! /bin/bash
for dir in * ; do
last=${dir##*_}
[[ -d $dir/$last ]] && rmdir "$dir/$last"
done
Bash - Get Current Directory or Folder Name (Without Full Path) In Bash Script
No need for basename, and especially no need for a subshell running pwd (which adds an extra, and expensive, fork operation); the shell can do this internally using parameter expansion:
result=${PWD##*/} # to assign to a variable
result=${result:-/} # to correct for the case where PWD=/
printf '%s\n' "${PWD##*/}" # to print to stdout
# ...more robust than echo for unusual names
# (consider a directory named -e or -n)
printf '%q\n' "${PWD##*/}" # to print to stdout, quoted for use as shell input
# ...useful to make hidden characters readable.
Note that if you're applying this technique in other circumstances (not PWD
, but some other variable holding a directory name), you might need to trim any trailing slashes. The below uses bash's extglob support to work even with multiple trailing slashes:
dirname=/path/to/somewhere//
shopt -s extglob # enable +(...) glob syntax
result=${dirname%%+(/)} # trim however many trailing slashes exist
result=${result##*/} # remove everything before the last / that still remains
result=${result:-/} # correct for dirname=/ case
printf '%s\n' "$result"
Alternatively, without extglob
:
dirname="/path/to/somewhere//"
result="${dirname%"${dirname##*[!/]}"}" # extglob-free multi-trailing-/ trim
result="${result##*/}" # remove everything before the last /
result=${result:-/} # correct for dirname=/ case
Remove first directory components from path of file
You can use any of:
x=a/b/c/d
y=a/
echo ${x#a/}
echo ${x#$y}
echo ${x#*/}
All three echo commands produce b/c/d
; you could use the value in any way you choose, of course.
The first is appropriate when you know the name you need to remove when writing the script.
The second is appropriate when you have a variable that contains the prefix you need to remove (minor variant: y=a; echo ${x#$y/}
).
The third is the most general - it removes any arbitrary prefix up to the first slash. I was pleasantly surprised to find that the *
worked non-greedily when I tested it with bash
(version 3.2) on MacOS X 10.6.6 - I'll put that down to too much Perl and regex work (because, when I think about it, *
in shell doesn't include slashes).
Related Topics
Extracting Variable in Yaml from a Shell Script
Using Scanf into Global or Local Variables (On the Stack), 32-Bit Calling Convention
How to Move First Column to Last Column in Unix
Print the Directory Where the 'Find' Linux Command Finds a Match
How to Increase Openfabrics Memory Limit for Torque Jobs
Perf Top Result About Nested Functions
How to Extract Patterns Form a Text Files in Shell Bash
File Glob Patterns in Linux Terminal
Webpack Build Fails with Gitlab-Ci
How to Make My .Bat File Run Linux Command to Remote Linux
Qserialport Cannot Open Tty After Application Has Previously Been Run by 'Root'
Shebang Not Working to Run Bash Scripts in Linux
Bash - How to Match Files Names to Use in Loop
Diff (Gnu Diffutils) 3.6 Exclude Directory
Output Stdout and Stderr to File and Screen and Stderr to File in a Limited Environment
How to Get Docker Commands to Run in the Background with Nohup