"test*" file creation.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "test*" file creation.
# 1  
Old 09-28-2012
Java "test*" file creation.

Hello All,

I am not well-versed with Unix commands. While practicing I got following situation.

My current working directory is empty and firstly, I created a file “test*” then I can see the file got created:
Code:
> ls -ltr
total 0
>
> touch test*
>
> ls -ltr
total 0
-rw-rw-rw-    1 kimxk    users             0 Sep 28 12:41 test*


Then I removed “test*” file and created two new files named “test1*” and “ test2*”:
Code:
> rm *
>
> touch test1* test2*
> ls -ltr
total 0
-rw-rw-rw-    1 kimxk    users             0 Sep 28 12:42 test2*
-rw-rw-rw-    1 kimxk    users             0 Sep 28 12:42 test1*


But again when I tried to create “test*” file this file is not created:
Code:
> touch test*
> ls -ltr
total 0
-rw-rw-rw-    1 kimxk    users             0 Sep 28 12:42 test2*
-rw-rw-rw-    1 kimxk    users             0 Sep 28 12:42 test1*
>

Query: Why it is not creating “test*” file if “test1*” and “ test2*” files already exists?

Many thanks in advance.

Regards,
Manish

Last edited by Franklin52; 09-30-2012 at 05:54 PM.. Reason: Please use code tags
# 2  
Old 09-28-2012
This probably has to do with you use of *. The file name test* refers to any file that starts with test and has anything after it, like.

test1
test2
test.txt
test.whatever
...

you should probably not use the * to create a single file. It really doesn't make any sense since its purpose is to refer to anything that starts with whatever is before the *, or anything that has a specific ending.

You can use * to do things like "delete all files with the extension .txt"

rm *.txt

or copy everything that starts with 44 to a new directory

cp 44* ./newdir/

and things like that.

LMHmedchem
# 3  
Old 09-29-2012
You need to know that whenever you type in anything on the command line, your shell interprets the command line first and then the corresponding commands are called.
  • When your current directory is empty and you issue the command touch test*:

    The * character is special to the shell; it's a wildcard which tries to match 0 or more occurrences of any character. So, test* tries to match file-names (in the current directory) starting with test followed by 0 or more occurrences of any character. Since the shell cannot find any such file-names, the pattern is not expanded and the command line remains touch test* and thus, touch creates the file test*.

  • rm *

    Now, * matches test* and the command line becomes rm test* and the file is removed.

  • touch test1* test2*

    No file-names match the 2 patterns and the command-line remains as touch test1* test2* and the 2 files are created.

  • touch test*

    Now, test* matches the 2 files created by 3. So, the command-line becomes touch test1* test2* and the modification and access times of these 2 files are changed by touch.
    If you need to create a file name test*, you'll have the escape interpretation of * by the shell by using one of the following ways (or their variations):
    • touch test\*,or
    • touch test'*'
It is dangerous to include * in file-names.

Last edited by elixir_sinari; 09-29-2012 at 11:19 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 09-29-2012
Echo is nice debug command. Easy to see how shell expand the line before execute it.
Code:
echo touch test*

# 5  
Old 10-01-2012
Thank you all for the explanations.
I understand that it is dangerous to include * in file-names but only trying to understand some basic working knowledge (behind the scene)

I got to know somewhere that:
rm *\* --> is the script that removes all files that end with an asterisk in the name.
rm *\** --> is the script that removes all files that contain an asterisk in the name.

example:

> touch test test1* test2* make*a*chance create*all
>
> ls -l
total 0
-rw-rw-rw- 1 kimxk users 0 Oct 01 11:32 create*all
-rw-rw-rw- 1 kimxk users 0 Oct 01 11:32 make*a*chance
-rw-rw-rw- 1 kimxk users 0 Oct 01 11:32 test
-rw-rw-rw- 1 kimxk users 0 Oct 01 11:32 test1*
-rw-rw-rw- 1 kimxk users 0 Oct 01 11:32 test2*
>
===================== rm *\* =================================

> rm *\*
>
> ls -l
total 0
-rw-rw-rw- 1 kimxk users 0 Oct 01 11:32 create*all
-rw-rw-rw- 1 kimxk users 0 Oct 01 11:32 make*a*chance
-rw-rw-rw- 1 kimxk users 0 Oct 01 11:32 test
>
=====================================================


==================== rm *\** ============================

> rm *\**
>
> ls -l
total 0
-rw-rw-rw- 1 kimxk users 0 Oct 01 11:37 test
>
======================================================


I understand that in both the commands 1st '*' is meant to read and remove all the files current directory but not able to understand how the 2nd & 3rd '*' impacting this.


Many thanks in advance.

Regards,
Manish
# 6  
Old 10-02-2012
I like more to talk about file generation.
* in the filename is only char *.
*\* or *'*' start with 0-n chars (not begin .) and last char is *
=> * , 1*1*1*, **, ***, ****, all match.
Sameway dos a'like *.* result is
=> a.b.c.d match : start with 0-n some chars, then is dot and after that is 0-n chars.
* include all chars including *.

Use echo to test
Code:
echo .?*
echo *\**
echo *.*

Why echo is good ?
Because command line is parsed by shell, including file name generation and the result has given for command. So echo print out all arguments = what shell has done for the command line before give it to the command.
# 7  
Old 10-02-2012
Thanks a lot for the explanation.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 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. Shell Programming and Scripting

Shc : trying to test functionality "test" compiling but can not execute

I am testing shc to see if it would help with my need. Im at a point where Im trying to compile and test the "test.ksh" file that comes in the tar ball : shc-3.8.9> shc -v -r -f test.ksh shc shll=ksh shc =-c shc =exec '%s' "$@" shc = shc opts= shc: cc test.ksh.x.c -o test.ksh.x... (7 Replies)
Discussion started by: popeye
7 Replies

4. UNIX for Dummies Questions & Answers

What is the significance of sh -s in ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh?

Please can you help me understand the significance of providing arguments under sh -s in > ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh (4 Replies)
Discussion started by: Sree10
4 Replies

5. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

6. 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

7. AIX

mkinstallp package creation failing "no such file: ./usr/lpp/<package name>/inst_root"

Hello, I'm trying to build a (bff) package from an already installed program (clam antivirus) using mkinstallp. However, mkinstallp fails with "no such file: ./usr/lpp/<package name>/inst_root" I'm not sure why all files get created ok except for these particular ones. Any help would be... (2 Replies)
Discussion started by: omonte
2 Replies

8. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question