7

how to manipulate the filename using shell scripts

 2 years ago
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

advertisements

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?

I use shell-command a lots (default binded to M-!), but often I'm doing stuff to the buffer I'm currently editing. Instead of typing the buffer name (and no filename completion is available in shell-command, alas!), it'd be nice to have a shortcut ke

How to combine CSV files using shell script / Python

This question already has an answer here: Left outer join on two files in unix 2 answers I have two CSV files input.csv: id,scenario,data1,data2,result 1,s1,300,400,"{s1,not added}" 2,s2,500,101,"{s2 added}" 3,s3,600,202, output.csv id

How to manipulate $ PATH elements in shell scripts?

Is there a idiomatic way of removing elements from PATH-like shell variables? That is I want to take PATH=/home/joe/bin:/usr/local/bin:/usr/bin:/bin:/path/to/app/bin:. and remove or replace the /path/to/app/bin without clobbering the rest of the vari

How can I store mysql databases in Linux using shell scripts

How can i store mysql databases in linux using shell scripting script: mysql -uusername -hhostname -ppassword -e "show databases" I think you want this: http://lists.mysql.com/mysql/96132 mysql> use mysql Database changed mysql> tee /tmp/m

How do I remove characters from a line to a particular character using shell scripts

I have a file called output.txt and it caontains a line "test[1,2,3]: [x+y=0]" I want to delete all the characters upto '['. and delete ']' as well. I want to have only x+y=0 in output.txt using shell script. I can not delete some fixed no. of c

Automated Web Tests using shell script for Internet Explorer and Firefox

I know I should be using other tools (like Selenium) for automated web tests but I have to use shell script. My problem is I need to open URLs in multiple IE and Firefox tabs. This is straight forward in Firefox but there doesn't seem to be any comma

Sqlplus access and e-mail access using shell script

I am fairly beginner level at shell scripts and following are the details.. Am looking for the best way to fire sql queries and and carry out some logic based on that data. I've used the following snippet.. shellvariable=sqlplus $user/$passwd <<END

list all folder names in the directory - Unix shell script

How can I get a list of all folder using shell script, directory is present in HDFS. Here is what i have tried : #!/bin/ksh echo " loading folders..." count=0 for folder in `hdfs dfs -ls -d /user/data/*` do if [ -d "$folder" ] then cou

Merge two property files using shell scripts

How to merge two properties file, using shell scripts for example : - if i have two properties file like first.properties /test/file="anish" /test/version=3.0 second.properties /test/author=nath /test/version=2.0 if i merge first.properties over

Is there a way to call the macro vim shell script?

I am doing data processing in unix environment using shell script and vim macros. Here are the steps: I use grep to get some useful lines from log files. Macros inside vim are used to manipulate the data. Again, use shell script to process the data.

How to rewrite the code using jQuery

How to rewrite the code using jQuery? <script type="text/javascript"> window.onload = function(){ var array = document.getElementsByTagName('div'); for (var i=0; i<array.length; i++) { (function(i) { array[i].onclick = function() { aler

How to pass the argument to Mongo Script

I've been using mongo and script files like this: $ mongo getSimilar.js I would like to pass an argument to the file: $ mongo getSimilar.js apples And then in the script file pick up the argument passed in. var arg = $1; print(arg); Use --eval and us

Managing libraries using Shell Script

Hey I was trying to simulate a simple Library management system using Shell Script as part of my college UNIX assignment but I'm having a weird errors in script. Here is my script menu_choice="" record_file="bookRecords.ldb" temp_file=

without page reload how to insert the data using javascript below

Using e.preventDefault(); values not insert in to database,if comment e.preventDefault(); data inserted in to database, without page reload how to insert the data using below javascript code please suggest me Insert form <form class="form-horizont

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK