Moving file using test command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving file using test command
# 1  
Old 04-29-2009
Moving file using test command

My process creates file like

abc.20090427.txt i.e abc.date.txt

next time when my process it has to detect if any previous "abc" exist.
If exist then move to archive and create a new abc file.


I am using test command but it doesnt allow wild card.
Code:
if [[ -f abc.*.txt ]]
then 
mv abc.*.txt archive/abc.*.txt
fi

case statement will not work in this scenario since it cannot detect whether the file "abc" already exists.

Appreciate your help
# 2  
Old 04-29-2009
Quote:
Originally Posted by zenith
My process creates file like

abc.20090427.txt i.e abc.date.txt

next time when my process it has to detect if any previous "abc" exist.
If exist then move to archive and create a new abc file.


I am using test command but it doesnt allow wild card.
Code:
if [[ -f abc.*.txt ]]


That is not the test command; the test command is test or [.
Quote:
Code:
then 
mv abc.*.txt archive/abc.*.txt
fi

case statement will not work in this scenario since it cannot detect whether the file "abc" already exists.

Code:
is_file() { [ -f "$1" ]; }

if is_file abc.*.txt
then
  mv abc.*.txt archive/
fi

# 3  
Old 04-29-2009
Quote:
Originally Posted by cfajohnson
That is not the test command; the test command is test or [.
Code:
is_file() { [ -f "$1" ]; }
 
if is_file abc.*.txt
then
  mv abc.*.txt archive/
fi


In my directory i have
abc.txt
def.txt

Code:
#!/bin/ksh
is_file() { [ -f "$1" ]; }
if is_file *.txt
then
  mv *.txt archive
fi

when i run the above code the files are still there
# 4  
Old 04-29-2009
Quote:
Originally Posted by zenith
when i run the above code the files are still there

What is the output of this script:

Code:
is_file() { [ -f "$1" ]; }

printf "%s\n" *.txt
if is_file *.txt
then
  mv *.txt archive
fi
printf "%s\n" *.txt


This version of is_file is a little more robust:

Code:
is_file() {
  for f; do
    [ -f "$f" ] && return
  done
  return 1
}

# 5  
Old 04-29-2009
Quote:
Originally Posted by cfajohnson
What is the output of this script:

Code:
is_file() { [ -f "$1" ]; }
 
printf "%s\n" *.txt
if is_file *.txt
then
  mv *.txt archive
fi
printf "%s\n" *.txt


This version of is_file is a little more robust:

Code:
is_file() {
  for f; do
    [ -f "$f" ] && return
  done
  return 1
}


Tried both doesnt gives any output.

I have two files with .txt at ending but they are not moved

Code:
#!/bin/ksh
is_file() {
  for f; do
    [ -f "$1" ] && return
  done
  return 1
}
printf "%s\n" *.txt
if is_file *.txt
then
  mv *.txt archive
fi
printf "%s\n" *.txt

Code:
#!/bin/ksh
is_file() {
  for f; do
    [ -f "$f" ] && return
  done
  return 1
}
printf "%s\n" *.txt
if is_file *.txt
then
  mv *.txt archive
fi
printf "%s\n" *.txt
~
~

# 6  
Old 04-29-2009
Quote:
Originally Posted by zenith
Tried both doesnt gives any output.

If there is no output, you don't have any *.txt files in the current directory.

Are you running the script in a different directory?
# 7  
Old 04-29-2009
Quote:
Originally Posted by cfajohnson
If there is no output, you don't have any *.txt files in the current directory.

Are you running the script in a different directory?

I am running in the directory where i have two .txt files
ABC.txt
DEF.txt

Script runs no output and files are not moved

HTML Code:
$ksh -x test
+ + basename test
cmd=test
+ test
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Moving lines of file to command line

I have a file in which each line is the name of another file. Is there a way to serve them to the command line? For example, if the file contains file1.txt file2.txt file3.txt ... file9.txt is there a way to insert them in the command as a batch? If I ran a command like grep... (4 Replies)
Discussion started by: wbport
4 Replies

2. Shell Programming and Scripting

Test command:Duplicate Header row in Log File

I have a script that is inventorying (not sure if thats a word) my environment. It goes out and pulls Hostname OS, IP, and ENV (dev, prod, etc)..then spits all that to a logfile At the top of my script i have a check to see if the logfile exist. ] || touch $LOGFILE && echo "ENV" "\t"... (3 Replies)
Discussion started by: nitrobass24
3 Replies

3. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

4. Shell Programming and Scripting

Moving 100K file to another folder using 1 command

Hi, I need to move 1000s of files from one folder to another. Actually there are 100K+ files. Source dir : source1 Target dir : target1 Now if try cp or mv commands I am getting an error message : Argument List too long. I tried to do it by the time the files are created in the source... (6 Replies)
Discussion started by: unx100
6 Replies

5. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

6. Shell Programming and Scripting

awk command to test if a string is a file

What awk command will test a string to determine if it is a valid file name? With the following awk statement I isolate the lines from the inputfile that might contain a filename, then I attempt to test the possible filename which is always in $4 from each line. However it is not working at all... (4 Replies)
Discussion started by: Arsenalman
4 Replies

7. Shell Programming and Scripting

moving files alone using mv command??

Is there a way to move the files ALONE from one dir to another dir? In my source dir,I have files as well as directories.I want to move the files alone to another dir and the directories should remain undisturbed. If I use mv * < target dir> ,then the directories also moved. Any... (4 Replies)
Discussion started by: prasperl
4 Replies

8. Shell Programming and Scripting

moving files after ls -lt command

Can you please tell me how to find a file in a directory and then if it exists i need to place it in a different folder? I have to use "ls -lt" command I mean this command is something existing one . can you please tell me how i set the directory path. Like dir1/dir2/filename.csv to... (8 Replies)
Discussion started by: pochaman
8 Replies

9. UNIX for Dummies Questions & Answers

Command file for moving emails

hey guys. I'm trying to create a command file that moves my emails to a specified folder according to a keyword that is in the subject. like move emails with "hey" in the subject to a folder that exists in my mail folder named "hey". how do I go about that? thanx. (9 Replies)
Discussion started by: ~samantha89~
9 Replies

10. Shell Programming and Scripting

file descriptor test command

Can someone delve more into what below test command do? if ; then I know -t is for file descriptor -t file descriptor FD (stdout by default) is opened on a terminal But not sure how to relate to it.. $1 is supposedly first argument, so let's say if scriptname:... (2 Replies)
Discussion started by: convenientstore
2 Replies
Login or Register to Ask a Question