How do I use the cut command to only print the directories?

How would I use ls -l | cut to just print the directories part from the ls -l command?

It depends where you are starting from. Perhaps you are starting at the wrong place with ls -l . Would you be better with cut the output from ls or ls -1 ? If you pipe ls to another command or into a file, they are equivalent anyway. They both give you just a list file/directory names you have run it against.

If you are getting a listing and trying to select the directories from it, that's different.

Can you tell us clearly what you are trying to achieve and we can probably suggest a selection of ways to do it, but it depends where you are starting and what output you have already.

If you could paste some samples into the thread wrapped in CODE tags then we can see what we are working with.

Kind regards,
Robin

Trying to learn about the cut command and use it to cut a specific column from the

ls -l 

command, just wondering what I would have do to cut specific parts from text output, in this case the directories at the end of the output.

For example:

This output:

drwx------   3 user1  user  4096 19 Jan  2016 Applications
drwx------   2 user1  user  4096 22 Mar  2016 Desktop
drwx------   5 user1  user  4096  6 Oct 09:45 Documents
drwx------   3 user1  user  4096  6 Oct 09:10 Downloads
drwxr-xr-x@ 31 user1  user  4096  6 Oct 08:55 Library
drwx------   2 user1  user  4096 12 Jan  2016 Movies
drwx------   2 user1  user  4096 12 Jan  2016 Music
drwx------   2 user1  user  4096 12 Jan  2016 Pictures

Then by using the cut command should look like

Applications
Desktop
Documents
Downloads
Library
Movies
Music
Pictures

Thanks.

Hello steezuschrist96,

Could you please try following commands and let me know if this helps you.

ls -d */
OR
find -maxdepth 1 -type d -printf '\n%P'

Thanks,
R. Singh

Assuming you are trying to learn about the process rather than actually wanting to do anything with it directly, then......

.... If you are splitting on character count (so character 47 onwards) you could:-

ls -l | cut -c47-

..... If you are splitting on fields, you are after field 9, so:-

ls -l | tr -s " " | cut -f9 -d " "

The tr -s " " is to ensure that multiple spaces are treated as a single space.

If you actually want to use this as input, then RavinderSingh13 gave you far more sensible answers than getting a long listing and chopping it up.

I hope that this helps,
Robin

1 Like

Beware, it likes to resize the field widths when there are large files.

Juha

1 Like

For two reasons, cut may not be the tool of choice here:

  • you can't select by field, as a non-predictable number of spaces in the owner, group, and size field doesn't allow to reliably select the file name field. tr anslating spaces can deteriorate file names, btw.
  • you can't select by character/byte position, as esp. the size field can be adapted to large numbers.

So you might need to fall back to a text utility as sed , awk , and so on.
But, la | awk '{print $9}' wouldn't help either, in case of file name containing spaces. You'd need to resort to sth like

la | awk '{sub ($1 FS $2 FS ".*" $8 FS, _); print}'

, but that also might still need some polishing.

1 Like

Yes, apologies, I never thought of varying column widths when the byte count grows or the file age exceeds 6 months.

I suppose I could argue that if people insist on putting spaces in file names then, ..........

Robin

It would be worth a new thread, how to tidy up filenames after unpacking an archive with funny characters. Everyone probably has their own quick fix :slight_smile:

Juha

When learning how to use UNIX utilities, one key thing to learn is to use the right tool for the job.

It isn't just file sizes and whitespace characters in filenames that are a problem here. The ls -l command output width will also float up or down depending not only on the widest file size, but also on the widest link count, widest user name, and widest group name. Note that even if the timestamp on a file is six months old or in the future, that won't make the ls -l output width or field count change in some versions of the ls utility (at least in the POSIX locale and any English language locale I've used; but it certainly could be a factor in other locales).

And, then, since the user wants the names of directories (not the names of all files) and the 1st line of output from the ls -l command is a something like:

total xxx

where the word "total" may vary depending on the locale and xxx is the number of filesystem blocks occupied by files in the current directory, the cut utility by itself just is not capable of performing the requested task.

If you just want the names of unhidden directories located in the current directory to be printed on separate lines (as long as none of the directory names contains a <newline> character; which would also be a problem when using ls -l ), you can easily do that just with shell built-ins in most POSIX-conforming shells:

printf '%s\n' */

or, if the trailing slash in the output that produces is a problem:

for i in */;do printf '%s\n' "${i%/}";done

If trailing slashes aren't a problem and there might not be any directories in the current directory, you could also use:

ls -d1 */ 2> /dev/null

Note that that is the digit 1, not the lowercase letter l in the options after -d .

1 Like