"Mv" command does not work in loop, but works manually


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers "Mv" command does not work in loop, but works manually
# 1  
Old 11-05-2019
"Mv" command does not work in loop, but works manually

Hi there, this may be a beginner's error, but I've been unable to find a solution on my own and by googling, and now I am really stuck on it.

I am simply trying to move directories called for example CAT_Run01.ica to a directory with the corresponding number, Run01, in the same directory.

For some reason the loop does not work
Code:
		nrun=`ls -d CAT_Run??.ica | wc -l`							
		for run in `seq 1 $nrun`; do
			mv CAT_Run0$run.ica Run0$run
                done

The "mv" command works manually: if I instead of looping type "run=1", then the line "mv CAT_Run0$run.ica Run0$run" will work and move "CAT_Run01.ica/" to "Run01/". The sequence works, it prints out the correct numbers.

But for some reason, when running the loop, I get the error:
"-bash: 0.ica: command not found"

Best,
Andreas
Psychology student just trying to work with some data

Last edited by vbe; 11-05-2019 at 03:18 PM..
# 2  
Old 11-05-2019
The file names and the sequence number are not necessarily synchronised; e.g. a single file CAT_Run02 will yield an nrun value of 1 and so the seq will never produce a match. A different approach should be chosen.


Howsoever, this would not explain the error message you saw. Nor can I. Pls run the script again with the -x (xtrace) option set, and post the result.
# 3  
Old 11-05-2019
Perhaps you want
Code:
for f in CAT_Run??.ica; do
  test -f "$f" &&
  echo mv "$f" "${f#CAT_}"
done

Important: the arguments to mv (and test) must be in "quotes".

Last edited by MadeInGermany; 11-05-2019 at 06:03 PM.. Reason: Added a test -f
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Red Hat

"/usr/sbin/hpacucli ctrl all show" command does not work

Dear Concern, We have observed that following command stuck/does not work in some RedHat nodes. Please advise us to troubleshoot the issue. /usr/sbin/hpacucli ctrl all show Note: HP Array Configuration Utility CLI for Linux 64-bit With Best Regards, Md. Abdullah-Al Kauser (3 Replies)
Discussion started by: makauser
3 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. AIX

"/" doesn't work on command prompt for searching commands last typed

When I use "/" to look for a particular command that I typed in the current session it says D02:-/home/user1/temp> /job ksh: /job: not found. D02:-/home/user1/temp> previously it used to fetch all the commands which had job in it.. for example subjob, endjob, joblist etc... may I... (7 Replies)
Discussion started by: meetzap
7 Replies

6. Shell Programming and Scripting

Silly question - how does the "mv" command work?

I know this may sound really elemental, but I'm trying to figure out if I'm correct. I have a script that moves a file from a temp directory to (what I am calling) a pickup directory. On another machine, I have this "other program" that scans the contents of the pickup directory for a... (5 Replies)
Discussion started by: gseyforth
5 Replies

7. UNIX for Advanced & Expert Users

sometimes "ps -elf" command doesn't work

when i give "ps -elf" or "ps" system gets hung. if i press "^c" come out from it... pls help..what should i do to get it resolved. thanks CKanth (4 Replies)
Discussion started by: srikanthus2002
4 Replies

8. Shell Programming and Scripting

How to get Find command work with a variable passing "*" value?

Hi guy, I have a problem to pass a variable containing '*' value to FIND command. below is the script. It doesn't work by submit below command: rmf.sh name '*.txt' or rmf.sh name *.txt I've tried either optn="-name '$2'" or optn="-name $2"., and there is no luck. ### (script... (5 Replies)
Discussion started by: unxuser
5 Replies
Login or Register to Ask a Question