0

I am looking to display the 8th file name in a directory using ls -l and pipe for example ls -l | wc -1 would give me the count but what i want to have returned is the 8th file name in the directory list whatever it is. i have looked at grep and wildcards but still do not see which command would give me the result i am looking for.

Thanks

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
Erich
  • 1

4 Answers4

4

try

ls -l | head -n 9 | tail -n 1

if you want only name you can use cut at the end

ls -l | head -n 9 | tail -n 1 | tr -s ' ' | cut -d' ' -f8

tr is to replace multiply spaces and tabs by one space

jcubic
  • 3,123
  • 4
  • 25
  • 23
1

Another way is like this.

ls -l | sed -n '9p'

If you only want the file or directory name then use this.

ls -l | sed -n '9p' | awk '{print $9}'

jcubic are you sure this is what you wanted?

ls -l | head -n 9 | tail -n 1 | tr -s ' ' | cut -d' ' -f8

I would think he would want the file or directory name like this.

ls -l | head -n 9 | tail -n 1 | tr -s ' ' | cut -d' ' -f9

cokedude
  • 449
0
ls | awk 'NR==8'
0

Just don't use -l

ls | head -8 | tail -1

You get file name, ls orders files same as ls -l.