This is a shell expansion problem.
Your shell will interpret wildcards before passing it to your process. For example:
script.sh
#!/bin/bash echo $1 $2 $3
Running above using a template:
> ./script.sh E* Eva Eve Evolve
If you want to pass an argument without interpreting the interpreter, first of all, you have to quote it:
> ./script.sh 'E*' E*
Best solution using find :
What you are actually trying to do is get a list of all the files and folders in a given directory, in the order in which the change time changed (oldest). The ls output is known to be painful for parsing. It is preferable to use the powerful and flexible find .
This is one liner:
> find ./ -maxdepth 1 -printf "% A@ %f\0" | sort -z -n | while read -d '' date line; do echo "$line"; done
Most likely, it is mysterious, but it makes sense after an explanation.
- Find all files in this directory without
find ./ -maxdepth 1 - For each file, print the last modified time in the second
-printf "% A@ - and their file name, delimited by null characters
%f\0" - Sort strings separated by zeros, last modified time (in numerical form)
sort -z -n - For each zero shared line, assign a timestamp “date” and the rest of the line “line”:
while read -d '' date line - Print the line
echo "$line"
For example, in my directory:
> ls -l total 4296 drwxr-xr-x 2 bfer cvs 4096 2012-03-05 15:49 colortry drwxr-xr-x 3 bfer cvs 4096 2012-03-27 15:05 githug drwxr-xr-x 3 bfer cvs 4096 2012-03-12 17:18 hooks-bare drwxr-xr-x 3 bfer cvs 4096 2012-03-28 12:38 java -rw-r--r-- 1 bfer cvs 4025413 2012-03-27 12:53 mozdebug drwxr-xr-x 2 bfer cvs 4096 2012-02-16 12:54 mustache_bug_demo -rwxr-xr-x 1 bfer cvs 113 2012-03-30 12:20 try.sh > find ./ -maxdepth 1 -printf "% A@ %f\0" | sort -z -n | while read -d '' date line; do echo "$line"; done mozdebug colortry hooks-bare mustache_bug_demo githug java try.sh ./
If you don’t need the result ./ , just take it out of your final set.
updated:. With Sorpigal's suggestion, process filenames with newlines.
Next, note the shell extension
This is the same as brackets and ampersands. For example, the following curl commands will not work as expected:
> curl example.com/site?q=hello&name=bob > echo 23/(7/98) | bc
Because ampersands and brackets will be interpreted by the shell before they are passed to the process as arguments.
For proper operation you will have to quote the arguments:
> curl "example.com/site?q=hello&name=bob" > echo "23/(7/98)" | bc