how to manipulate the filename using shell scripts
source link: https://www.codesd.com/item/how-to-manipulate-the-filename-using-shell-scripts.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
how to manipulate the filename using shell scripts
My source file is located in /user/public/test.txt
The file test.txt
should be modified to user_test.txt
and copied to /home/home/user_test.txt
An alternative interpretation of the question:
file=/usr/public/test.txt
base=user_$(basename $file)
dirn=$(dirname $file)
mv $file $dirn/$base
cp $dirn/$base /home/home/$base
Since you stipulate bash
, the portable code above can use:
file=/usr/public/test.txt
base=user_${file##*/}
dirn=${file%/*}
mv $file $dirn/$base
cp $dirn/$base /home/home/$base
The code takes liberties because it notes that there are no spaces or newlines or other special characters in the file names. If you have to deal with those, you have to be a lot more careful (liberal) with the use of double quotes.
Related Articles
In Emacs, how to insert the filename in shell-command?
How to combine CSV files using shell script / Python
How to manipulate $ PATH elements in shell scripts?
How can I store mysql databases in Linux using shell scripts
How do I remove characters from a line to a particular character using shell scripts
Automated Web Tests using shell script for Internet Explorer and Firefox
Sqlplus access and e-mail access using shell script
list all folder names in the directory - Unix shell script
Merge two property files using shell scripts
Is there a way to call the macro vim shell script?
How to rewrite the code using jQuery
How to pass the argument to Mongo Script
Managing libraries using Shell Script
without page reload how to insert the data using javascript below
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK