I found this shell script($1
,$2
are directories):
mv "$2" "$1" || exit # Make $2 a subdirectory of $1
cd "$1/$(basename "$2")" || exit # Change directories for simplicity
for f in *; do
mv "$f" "${f%.*}.txt" # Add or change the extension
done
it moves the second directory in the first one (so the second directory becomes a subdirectory of the first one) and all the files from the second directory will have a ".txt" extension. I can't figure out why cd "$1/$2"
won't work doesn't basename strip the filename from a given directory? Could someone explain how this part:
cd "$1/$(basename "$2")"
works? Also $2 needs to be in separate quotes("$2"
) because of basename?
The basename
removes the leading directory information. Assume that the sript is called with arguments like this:
$$> script usr/local/foo usr/local/bar
Now the $2
is equal to usr/local/bar
.
If you only do cd $1/$2
, the command will look like this:
cd usr/local/foo/usr/local/bar
But in reality, it should be
cd usr/local/foo/bar
It is removing this directory prefix from usr/local/bar
is whhat is done by basename
.
https://www.geeksforgeeks.org/basename-command-in-linux-with-examples/