for loop on ssh bash to rename multiple files - linux

Have created set of files
test1, test2, test3
the above files are on different server name xyz.com
If at all i need to rename the files with prefix
ex: test1 > old_test1
test2 > old_test2
test3 > old_test3
I tried:
#!/bin/bash
ssh $xyz.com -t -t /bin/bash <<EOF
cd /tmp
for i in {test*}
do
mv -v {i} "{i/test/old_test}"
ls -ltr
done
exit
but the output is just mv -v followed by list of files and nothing is happening (rename).

!/bin/bash
ssh $xyz.com 'bash' <<'ENDSSH'
cd /tmp
for i in {test*}
do
mv "${i}" "${i/test/old_test}";
ls -ltr
done
exit

change your rename-command to the following:
mv -v "$i" "${i/test/old_test}";
I tried your problems as follow.
In my directory are the following files:
test1
test2
test3
With this command:
for i in $(ls); do (mv "$i" "${i/test/old_test}"); done
i rename all files.
My files are now:
old_test1
old_test2
old_test3
Please change your for-loop like the this:
#!/bin/bash
ssh $xyz.com -t -t /bin/bash <<EOF
cd /tmp
for i in $(ls);
do
mv -v "$i" "${i/test/old_test}";
ls -ltr
done

Related

Rename files that have consecutive numbers for extensions

I have a directory with a few hundred files in the following format:
file.txt.1
file.txt.2
file.txt.3
file.txt.4
...
I need to rename these all to this format:
file1.txt
file2.txt
file3.txt
file4.txt
...
Use mmv, install sudo apt-get install mmv.
$ mmv -n '*.*.*' '#1.#3.#2'
file.txt.1 -> file.1.txt
file.txt.2 -> file.2.txt
file.txt.3 -> file.3.txt
Or use find and shell (POSIX sh/bash/Korn/zsh) parameter substitution expansion.
find . -type f -execdir sh -c 'num=${1##*.}; echo mv -v "$1" "file.${num}.txt" ' _ {} \;
Remove echo to perform actual rename on files.
Not sure if this is the best way to do what you're asking but it will rename all the files the way you need. Just save it to a file (rename.sh for example) then give it execution permissions (chmod +x rename.sh) and run with ./rename.sh
#!/bin/bash
for filename in file*; do
newFile=`echo $(basename $filename) | awk -F'.' '{print $1 $3 "." $2}'`
echo mv \"$filename\" \"$(dirname $filename)/$newFile\";
done | /bin/bash
If you wanted to run a dry-run, replace | /bin/bash with > renames.txt. This will save all the renamed files to the text file where you can review the changes.

Bash script to transfer all files from current directory to specific directory based on name

I have these files:
100-1.jpg
100-2.jpg
200-1.jpg
200-2.jpg
I want these to be transferred to specific folder based on filename
100/100-1.jpg
100/100-2.jpg
200/200-1.jpg
200/200-2.jpg
How do I do this?
What I have tried so far
cd ~/images
for f in *.jpg
do
mv -v "$f" ~/images/${f}/${f%}.jpg
done
how do I know I cut the string before the dash e.g 200-1 to 200 and store in a variable?
so I can do it like this
cd ~/images
for f in *.jpg
name="$f without the .jpg"
do
mv -v "$f" ~/images/${f}/${f%}.jpg
done
#!/bin/bash
cd ~/images
for f in *.jpg
do
mkdir -p ${f%-*}
echo ${f%-*}
mv "$f" ~/images/${f%-*}/${f%}
done

Bash script creates symlink inside directory aswell

I have a bash script:
for file in `find -mindepth 1 -maxdepth 1`
do
old=`pwd`
new=".${file##*/}"
newfile="$old/${file##*/}"
cd
wd=`pwd`
link="$wd/$new"
ln -s -f $newfile $link
cd $old
done
This is meant to create a symlink in the user's home directory with '.' prepended to all the files and directories in the current working directory. This works fine, however, it also creates a symlink inside any sub directories to the same directory. E.G. foo will now have a link at ~/.foo and foo/foo.
Is there any way I can fix this?
EDIT: Thanks to Sameer Naik and dave_thompson_085 I have changed it up a bit, but the problem still persists even when a alternate directory is given as an argument. It isn't a problem with sub directories it is that two links are being made, one to ~/.foo123 and one to foo123/foo123 not a link is being made to ~/ for foo1 and foo1/foo2.
for file in `ls -l | grep -v total | awk '{print $NF;}'`
do
old=`pwd`
wd=`cd $1 && pwd`
new=".${file}"
newfile="$wd/${file}"
cd
wd=`pwd`
link="$wd/$new"
ln -s -f $newfile $link
cd $old
done
Since you don't want recurse into sub-directories, try using
for file in `ls -l | grep -v ^d | awk '{print $NF;}'`
or use -type f in find to exclude subdirectories

Find shell script - logs (SUSE Linux, bash, if, find)

I have a problem with find command in shell script.
My script finds the archives then unpacks some logs from it and tar the results move it level up and remove file that were created in process.
And there is a problem it will do all operation even when script didn't find archive or logs as a result it creating empty tar. with name:
date_patern_patern.tar
i was trying fix it using if-statement or && and || operators but i can't handle it could you please direct me how to do it ?
I have modified the script:
#!/bin/bash
printf " -----\nSerch date: $1 \n"
printf "Serch patern: $2 \n"
printf "serch patern: $3 \n -----\n\n"
printf "Serch archives:\n"
mkdir /cc/xx/logs/aaa/envelop/tmp
find archives/ -name "$1_*_messages.tar.gz" -exec cp {} tmp \;
ls -l tmp/$1_*_messages.tar.gz || exit 1
cd tmp
tar -xf $1_*_messages.tar.gz --wildcards --no-anchored "*$2_$3*"
printf "Find envelop:\n"
ls -l alsb-message/ || exit 1
mv alsb-message $1_$2_$3
tar -cvf $1_$2_$3.tar $1_$2_$3
mv $1_$2_$3.tar ..
rm *.gz
rm -R $1_$2_$3
cd ..
rm -r tmp
there is another problem i want my script to stop when it serch and doesn't find patern in example when schript is serching:
./serch_script.sh 20151110 patern2 patern3
and it doesn't find either parern2 or patern3 i want it to stop but it do again empyt .tar i tried to do like Prasanna told but it didn't work in this case,
Please replace following line
ls -l tmp/
with following line
ls -l tmp/$1_*_messages.tar.gz || exit 1
Basically with this code, you are running exit 1 if there are no files in tmp directory matching your criteria.

Does $PWD always equal $(realpath .)

Given
A modern Linux/UNIX/OSX (w/ realpath)
bash 4+ (even on OSX)
Is
"$PWD" == "$(realpath .)"
Always true?
It's pretty easy to test that this is not always the case.
$ mkdir /tmp/realdir
$ cd /tmp/realdir
$ echo $PWD
/tmp/realdir
$ ln -s realdir /tmp/fakedir
$ cd /tmp/fakedir
$ echo $PWD
/tmp/fakedir
$ realpath .
/tmp/realdir
so no, $PWD is not always the same as $(realpath .).
The bash manual indicates that the PWD variable is set by the built-in cd command. the default behaviour of cd is:
symbolic links are followed by default or with the -L option
This means that if you cd into a symlink the variable gets resolved relative to the symlink, not relative to the physical path. You can change this behavior for a cd command by using the -P option. This will cause it to report the physical directory in the PWD variable:
$ cd -P /tmp/fakedir
$ echo $PWD
/tmp/realdir
You can change the default behavior of bash using the -P option:
$ set -P
$ cd /tmp/fakedir
$ echo $PWD
/tmp/realdir
$ set +P
$ cd /tmp/fakedir
$ echo $PWD
/tmp/fakedir
This is of course notwithstanding the fact that you can assign anything you want to the PWD variable after performing a cd and it takes that value:
$ cd /tmp/fakedir
$ PWD=/i/love/cake
$ echo $PWD
/i/love/cake
but that's not really what you were asking.
It is not necessarily the case even when symbolic links are not used and PWD is not set by the user:
vinc17#xvii:~$ mkdir my_dir
vinc17#xvii:~$ cd my_dir
vinc17#xvii:~/my_dir$ rmdir ../my_dir
vinc17#xvii:~/my_dir$ echo $PWD
/home/vinc17/my_dir
vinc17#xvii:~/my_dir$ realpath .
.: No such file or directory
Note that under zsh, ${${:-.}:A} still gives the same answer as $PWD (the zshexpn(1) man page says about the A modifier: "Note that the transformation takes place even if the file or any intervening directories do not exist.").
Note that however, $PWD contains obsolete information. Using it may be a bad idea if some other process can remove the directory. Consider the following script:
rm -rf my_dir
mkdir my_dir
cd my_dir
echo 1 > file
cat $PWD/file
rm -r ../my_dir
mkdir ../my_dir
echo 2 > ../my_dir/file
cat ./file
cat $PWD/file
rm -r ../my_dir
It will output:
1
cat: ./file: No such file or directory
2
i.e. $PWD/file has changed.

Resources