I have a file with 2 characters in each line, like this:
RU
8A
1C
How can I read those characters into a unix command (in this case, forming an argument list for ls -t
)? I want something like this:
ls -t RU* 8A* 1C*
I have a file with 2 characters in each line, like this:
RU
8A
1C
How can I read those characters into a unix command (in this case, forming an argument list for ls -t
)? I want something like this:
ls -t RU* 8A* 1C*
If you want three separate runs of ls
:
while IFS= read -r line; do
ls -t "$line"*
done <input_file.txt
If you want just one run, with all the content globbed together:
set --
while IFS= read -r line; do
[ -n "$line" ] || continue
set -- "$@" "$line"*
done <input_file.txt
ls -t "$@"
Note that ls -t
is an unreliable tool. It won't work if you pass it more filenames than can be given to a single command invocation (and attempts to make it work using xargs
will give you multiple, individually-sorted lists in output). If on a platform with GNU tools (bash, and GNU find
and sort
), consider the following instead:
#!/usr/bin/env bash
predicates=( -false )
while IFS= read -r line; do
predicates+=( -o -name "${line}*" )
done <input_file.txt
while IFS= read -r -d' ' mtime && IFS= read -r -d '' filename; do
echo "Processing file $filename with timestamp $mtime"
done < <(find . -maxdepth 1 '(' "${predicates[@]}" ')' -printf '%T@ %P\0' | sort -z -n)
Useful links: