Retko
Retko

Reputation: 382

Use every line from cat command output as a file for (ls -l file)

I hope my question makes sense. I am quite new to this stuff.

I am using this piped command to make a list:

cat /etc/passwd | cut -d: -f7 |sort -n  |uniq  

The output is this:

/bin/bash   
/bin/false   
/bin/sync   
/no/shell  
/sbin/halt 
/sbin/nologin  
/sbin/shutdown 
/usr/sbin/nologin

I need to put every line in this output to this command:

ls -l /bin/bash
ls -l /bin/false 
ls -l /bin/sync

My output then looks like this:

-rwxr-xr-x. 1 root root 917576 11. bře  2013 /bin/bash   
-rwxr-xr-x. 1 root root  29920  8. kvě 09.45 /bin/false
-rwxr-xr-x. 1 root root  29940  8. kvě 09.45 /bin/sync

Please help me with this command.

Upvotes: 2

Views: 2683

Answers (4)

gniourf_gniourf
gniourf_gniourf

Reputation: 46823

Just for fun:

declare -A shell_you_re_nuts
while IFS=: read -r -a strawberry_fields; do
    shell_you_re_nuts[${strawberry_fields[6]}]=1
done < /etc/passwd
ls -l "${!shell_you_re_nuts[@]}"

Why? because I'm using bash's hash capabilities for the unique part, and then ls is smart enough to do the sorting for me.

And just for fun (again):

ls -l $( (IFS=$':\n'; printf '%.0s%.0s%0.s%0.s%0.s%.0s%s\n' $(</etc/passwd) ) | sort -u)

Upvotes: 0

Idriss Neumann
Idriss Neumann

Reputation: 3838

while read; do ls -l $REPLY; done <<< "$(cat /etc/passwd|cut -d: -f7|sort -u)"
# or
cat /etc/passwd|cut -d: -f7|sort -u|while read; do ls -l $REPLY; done

But if you just need to get some informations about this files (like permissions for example), prefer the stat command in this case. See man stat for more details. See this answer to know how to read a command output in a loop.

NB : sort -u already filter duplicates ;)

Upvotes: 0

delliottg
delliottg

Reputation: 4140

Another method:

echo ls -l `cat /etc/passwd | cut -d: -f7 | sort -n | uniq` | xargs

Edited again to add xargs at the end (which I'd spaced out earlier), now it gives the proper output, but it's essentially uʍop ǝpısdn's answer re-worked.

@uʍop ǝpısdn's answer works well.

Upvotes: 0

salezica
salezica

Reputation: 76909

xargs is ideal for this kind of command-line work. It executes the command given as argument repeatedly over the lines of input. Pipe your output into xargs ls -l:

cat /etc/passwd | cut -d: -f7 | sort -n | uniq | xargs ls -l

Upvotes: 2

Related Questions