1

I've a requirement in which I have to move particular files from one directory to another. I'm having the list of desired files in a file "req_files.txt"

prev_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder
new_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/New
cd $prev_dir
for i in `cat req_files.txt`
do
   mv $prev_dir/$i $new_dir
done

But I'm getting below error :

mv: cannot stat `/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder/dbo.account_address_adt.Table\r': No such file or directory

I tried the command for removing the line break problem, but still its not working:

sed -i 's/\r$//' test.sh
4
  • Is tr installed? It should be. If so, you can do tr -d '\r' < req_files.txt > no_dos_list.txt to remove the dos line returns. The next question is, do you have files with a space in their name?
    – Tigger
    Dec 29, 2016 at 6:32
  • Yes, tr is there. No my filename is not having any spaces. They have _ in the filenames. Dec 29, 2016 at 6:54
  • Please provide an example of the req_files.txt file
    – roaima
    Dec 29, 2016 at 13:54
  • Contents are like below : dbo.account_adt.Table.sql dbo.account_currency_adt.Table.sql dbo.account_sub_type_adt.Table.sql and so on Dec 30, 2016 at 5:56

5 Answers 5

1

First of all for removing \r from the file name you can try:

FILENAME=$(echo $FILENAME|tr -d '\r')
  • -d delete characters

And i suggest to use this way in iteration:

while read filename ; do
    filename=$(echo $filename |tr -d '\r')
    mv $prev_dir/$filename $new_dir
done < $prev_dir/req_files.txt
8
  • Thanks for the suggestion... but the loop is not iterating... It's only picking up the first file which it's finding, not the rest of them. Dec 29, 2016 at 7:06
  • 2
    @AbhisekSamanta not iterating ! its going throw every line in the file, and i think that is iterating ! Dec 29, 2016 at 7:18
  • 1
    How does your req_files.txt look like`? Add some lines to your question. It would also be save to double-quote the variables, just in case there are some special characters in the name.
    – Thomas
    Dec 29, 2016 at 9:59
  • @WissamAl-Roujoulah It's only working on the first file which it's getting from the "req_files.txt" and not working on rest of them..... Dec 30, 2016 at 4:54
  • @Thomas : req_files contains file names like below : Dec 30, 2016 at 4:55
0

I am getting done with below code shared yesterday please see o/p below: share your error

[root@rhel1 New_folder]# ll
total 12
-rw-r--r--. 1 root root   0 Dec  7 23:34 1
-rw-r--r--. 1 root root   0 Dec  7 23:34 3
-rw-r--r--. 1 root root   0 Dec 30 16:34 new folder1
-rw-r--r--. 1 root root   0 Dec 30 16:34 new folder2
-rw-r--r--. 1 root root   0 Dec 30 16:34 new folder3
-rw-r--r--. 1 root root  11 Dec  8 01:13 req_files.txt
-rwxr-xr-x. 1 root root 231 Dec  8 01:22 tesh.sh
-rw-r--r--. 1 root root 135 Dec  8 01:21 test1.sh
[root@rhel1 New_folder]# cat req_files.txt
New folder
[root@rhel1 New_folder]# vi req_files.txt
[root@rhel1 New_folder]# sh -x tesh.sh
++ echo -en '\n\b'
+ IFS='                                                                                                                                                                '
+ prev_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder
+ new_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/New
+ cd /cygdrive/c/Users/abhisek.samanta/Desktop/New_folder
++ cat req_files.txt
+ for i in '`cat req_files.txt`'
+ mv '/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder/new folder1' /cygdrive/c/Users/abhisek.samanta/Desktop/New/
+ for i in '`cat req_files.txt`'
+ mv '/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder/new folder2' /cygdrive/c/Users/abhisek.samanta/Desktop/New/
+ for i in '`cat req_files.txt`'
+ mv '/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder/new folder3' /cygdrive/c/Users/abhisek.samanta/Desktop/New/
+ IFS=
[root@rhel1 New_folder]# ll
total 12
-rw-r--r--. 1 root root   0 Dec  7 23:34 1
-rw-r--r--. 1 root root   0 Dec  7 23:34 3
-rw-r--r--. 1 root root  36 Dec 30 16:34 req_files.txt
-rwxr-xr-x. 1 root root 231 Dec  8 01:22 tesh.sh
-rw-r--r--. 1 root root 135 Dec  8 01:21 test1.sh
[root@rhel1 New_folder]# ll ../New/
total 0
-rw-r--r--. 1 root root 0 Dec  7 23:34 2
-rw-r--r--. 1 root root 0 Dec  7 23:34 4
-rw-r--r--. 1 root root 0 Dec  7 23:34 5
-rw-r--r--. 1 root root 0 Dec  8 00:56 New folder
-rw-r--r--. 1 root root 0 Dec 30 16:34 new folder1
-rw-r--r--. 1 root root 0 Dec 30 16:34 new folder2
-rw-r--r--. 1 root root 0 Dec 30 16:34 new folder3
[root@rhel1 New_folder]#
0

Thanks everyone for taking their valuable timing in sorting my problem out. I'd like to share the way I solved it :

prev_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/Table_scripts
new_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/New
cd $prev_dir
for i in `cat req_files.txt`
do
   sed -i 's/\r$//' $i 
   echo $i
   cd $prev_dir
   mv $i $new_dir
done

The same old short code ran without much tweaks. Thank you all for your sincere cooperation. :)

0

I know this is a couple of days old, but I'm not sure the shell loops are necessary, as the sed command can read through the whole file on its own, before using something like xargs to combine all the files into a single mv command:

prev_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/Table_scripts
new_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/New

cd ${prev_dir}
sed 's/\r$//' filelist | xargs mv -t $new

Here, using the given directories, and the sed command you have found works, then as everything is piped into xargs, it will add each line from the pipe onto the mv command, which can take multiple files and then move them all to the directory given with the -t flag

$ > cat movefile.sh 
new="$(pwd)/dir1/"
sed 's/\r$//' filelist | xargs mv -t $new
$ > cat -v filelist 
file1^M
file2^M
file3^M

$ > touch file{1,2,3}
$ > ls -l
total 16
drwxr-xr-x 2 grownups users 4096 Jan  1 22:29 dir1
-rw-r--r-- 1 grownups users    0 Jan  1 22:30 file1
-rw-r--r-- 1 grownups users    0 Jan  1 22:30 file2
-rw-r--r-- 1 grownups users    0 Jan  1 22:30 file3
-rw-r--r-- 1 grownups users   22 Jan  1 22:26 filelist
-rwxr--r-- 1 grownups users   61 Jan  1 22:29 movefile.sh
$ > ls dir1/
$ > ./movefile.sh 
$ > ls -l dir1/
total 0
-rw-r--r-- 1 grownups users 0 Jan  1 22:30 file1
-rw-r--r-- 1 grownups users 0 Jan  1 22:30 file2
-rw-r--r-- 1 grownups users 0 Jan  1 22:30 file3
$ > 
-1

Please use IFS which determines field separator.

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
prev_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder
new_dir=/cygdrive/c/Users/abhisek.samanta/Desktop/New
cd $prev_dir
for i in `cat req_files.txt`
do
   mv $prev_dir/$i $new_dir/
done
IFS=$SAVEIFS
1
  • It's only working on the first file which it's getting from the "req_files.txt" and not working on rest of them..... Dec 30, 2016 at 4:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .