I have picked up a folder name like this:
fname=${i##*/},
Where i is the 'runner' in a for loop, btw. Incidently, the i is the current directory. Now, I would like to use the $fname for renaming an other file. Something like this:
mv OLDFILE.Extension $fname.NewExtension
How can this be done in linux/bash?
Thanks
jd
Thanks. But I did:
mv OLDFILE.Extension $fname"NewExtension"
It worked out well.
jd
Related
How can I pass each one of my repository files and to do something with them?
For instance, I want to make a script:
#!/bin/bash
cd /myself
#for-loop that will select one by one all the files in /myself
#for each X file I will do this:
tar -cvfz X.tar.gz /myself2
So a for loop in bash is similar to python's model (or maybe the other way around?).
The model goes "for instance in list":
for some_instance in "${MY_ARRAY[#]}"; do
echo "doing something with $some_instance"
done
To get a list of files in a directory, the quick and dirty way is to parse the output of ls and slurp it into an array, a-la array=($(ls))
To quick explain what's going on here to the best of my knowledge, assigning a variable to a space-delimited string surrounded with parens splits the string and turns it into a list.
Downside of parsing ls is that it doesn't take into account files with spaces in their names. For that, I'll leave you with a link to turning a directory's contents into an array, the same place I lovingly :) ripped off the original array=($(ls -d */)) command.
you can use while loop, as it will take care of whole lines that include spaces as well:
#!/bin/bash
cd /myself
ls|while read f
do
tar -cvfz "$f.tar.gz" "$f"
done
you can try this way also.
for i in $(ls /myself/*)
do
tar -cvfz $f.tar.gz /myfile2
done
I want to add the ".sbd" after all files ending on ".utf8" in a directory
I do not want to replace the extensions, but really want to add them so the filenames will look like "filename.utf8.sbd"
I think I should adapt the following code, but don't manage to find out exactly how
for f in *.utf8 ; do mv "$f" "$f.sbd" ; done
Can anyone help me? I am very new to the command line
Thanks a bunch!
Your code should work if no file has spaces (or other "special" character) in the name and if the directory is not pathologically big.
In those cases, you can use something like this:
ls|grep '*.utf8$'|while read i; do mv "$i" "$i.sbd"; done
I have one file is POLL91.DWN then i want to rename xxxx.yyyymmddhhmmss.POLL91 (xxxx is the store name) it will become
GSTD.20151223121805.POLL91
mv POLL91.DWN GSTD.20151223121805.POLL91
If I understand your question, you can use date and a format string like
mv "POLL91.DWN" "GSTD.$(date +%Y%m%d%H%M%S).POLL91"
I want to change file names in a folder in a way like this:
previous form new form
one-1 to VAS-M0001-001
one-2 to VAS-M0001-002
one-3 to VAS-M0001-003
one-4 to VAS-M0001-004
Can anyone please suggest me a good way to do that?
I would just use a simple loop:
for f in one-*; do mv one-$f VAS-M001-000$f; done
Of course, you can use printf to format the number better (if you have more than 9 files)
rename has such a functionality
[username#hostname aa]$ touch one-1 one-2 one-3 one-4
[username#hostname aa]$ ls
one-1 one-2 one-3 one-4
[username#hostname aa]$ rename one- VAS-M0001-000 one*
[username#hostname aa]$ ls
VAS-M0001-0001 VAS-M0001-0002 VAS-M0001-0003 VAS-M0001-0004
I'm writing a script that needs to find a file in a directory based on the user input. That file contains a filepath, and I need to use that filepath as a variable so I can use it later in a mv command. So far :-
read x
path = `cat ~/filepaths/$x`
Later it needs to move a file from trash using the filepath read from this file
mv ~/trash/$x $path
Currently, it doesn't appear to work, and hangs when it runs. Is there something stupid I've missed here?
EDIT: Solved, was a stupid syntax mistake. Thanks for your help!
Remove the spaces around the assignment:
path=`cat ~/filepaths/$x`
or:
path=$(< ~/filepaths/$x)