Linux get directory listing

How to list folders using bash commands?

Since all directories end in / , this lists only the directories in the current path. The -d option ensures that only the directory names are printed, not their contents.

@Louis, — is conventionally used to mark the end of options, so that if a file is named -l ls won’t interpret it as the long listing format option.

Stephen Martin’s response gave a warning, and listed the current folder as well, so I’d suggest

find . -mindepth 1 -maxdepth 1 -type d 

(This is on Linux; I could not find -maxdepth and -mindepth in the POSIX man page for find)

Older question I know. While I would too initially turn to find for this task, I like the ls -d — */ option, as find will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1

Will list just folders. And as Teddy pointed out you’ll need -maxdepth to stop it recusrsing into sub dirs

Daniel’s answer is correct. Here are some useful additions, though.

To avoid listing hidden folders (like .git ), try this:

find . -mindepth 1 -maxdepth 1 -type d \( ! -iname ".*" \) 

And to replace the dreaded dot slash at the beginning of find output in some environments, use this:

find . -mindepth 1 -maxdepth 1 -type d \( ! -iname ".*" \) | sed 's|^\./||g' 

You’re «not supposed to» parse the output of ls, or so is said. The reasoning behind is that the output is intended to be human-readable and that can make it unnecessarily complicated to parse, if I recall.

if you don’t want ls or find, you may want to try filtering «*» with «[ -d ]».

I did just that, for some reason ls and find weren’t working (file names with spaces and brackets I guess, or somthing else I was overlooking), then I did something along the lines of

for f in * ; do [ -d "$f" ] && echo $f is indeed a folder ; done 

Just to emphasize a thing that confused me out here, in respect to glob patterns selection; say you have this:

$ cd /tmp $ mkdir testglob $ for ix in ; do mkdir testglob/mydir_$ ; done $ for ix in ; do touch testglob/myfile_$ ; done $ for ix in ; do touch testglob/mydir_$.txt ; done $ for ix in ; do mkdir testglob/otherdir_$ ; done $ tree testglob/ testglob/ ├── mydir_00 ├── mydir_00.txt ├── mydir_01 ├── mydir_01.txt ├── mydir_02 ├── mydir_02.txt ├── mydir_03 ├── mydir_03.txt ├── myfile_00 ├── myfile_01 ├── myfile_02 ├── myfile_03 ├── otherdir_00 ├── otherdir_01 ├── otherdir_02 └── otherdir_03 8 directories, 8 files 

So, say here you want to select only mydir* directories. Note that if you leave out the terminating slash, ls -d will list files as well:

$ ls -d testglob/mydir* # also `ls -d -- testglob/mydir*` testglob/mydir_00 testglob/mydir_01 testglob/mydir_02 testglob/mydir_03 testglob/mydir_00.txt testglob/mydir_01.txt testglob/mydir_02.txt testglob/mydir_03.txt 

. however, with a terminating slash, then only directories are listed:

$ ls -d testglob/mydir*/ # also `ls -d -- testglob/mydir*/` testglob/mydir_00/ testglob/mydir_01/ testglob/mydir_02/ testglob/mydir_03/ 

Источник

Читайте также:  Astra linux как подключить телефон

ls command in Linux/Unix

ls is a Linux shell command that lists directory contents of files and directories.

ls syntax

$ ls [options] [file|dir]

ls command options

option description
ls -a list all files including hidden file starting with ‘.’
ls —color colored list [=always/never/auto]
ls -d list directories — with ‘ */’
ls -F add one char of */=>@| to enteries
ls -i list file’s inode index number
ls -l list with long format — show permissions
ls -la list long format including hidden files
ls -lh list long format with readable file size
ls -ls list with long format with file size
ls -r list in reverse order
ls -R list recursively directory tree
ls -s list file size
ls -S sort by file size
ls -t sort by time & date
ls -X sort by extension name

ls command examples

You can press the tab button to auto complete the file or folder names.

List directory Documents/Books with relative path:

List directory /home/user/Documents/Books with absolute path.

List user’s home directory (e.g: /home/user):

List with long format and show hidden files:

Recursive directory tree list:

List only text files with wildcard:

ls redirection to output file:

List files and directories with full path:

ls code generator

Select ls options and press the Generate Code button:

Источник

Get a list of directory names with find

I prefer the listing without ./ . Is there a way to get find to output just the raw names? I tried sending the list to stat to format it but that just gives me the same result:

find . -type d -maxdepth 1 -print0 | xargs -0 stat -f '%N' 

a note that doesn’t answer the question (already well answered): on recent versions of find , you will get a warning if you use a global option like -maxdepth 1 after an argument like -type d ; it is now recommended to reverse the order to find . -maxdepth 1 -type d

9 Answers 9

With GNU find you can use the -printf option:

find . -maxdepth 1 -type d -printf '%f\n' 

As noted by Paweł in the comments, if you don’t want the current directory to be listed add -mindepth 1 , e.g.:

find . -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 

I cannot find any mention of the -printf option in the latest POSIX spec and I wonder how portable it is .

Читайте также:  Linux вход и выход

@rahmu: This was tested with GNU find. None of the printf options are mentioned by POSIX and they do not seem to be supported by some of the other find versions. GNU find can be installed in most places, so in that sense it is portable.

 find . -maxdepth 1 -type d | cut -c 3- 

Will give you the names, one per line, without any slashes

@downvoter .. a downvote without explanation doesn’t help anyone (OP, SO or me). This is a functional solution to OP’s question. I am happy to correct an error if pointed out or improve the answer, but that requires constructive feedback rather than just an anonymous «click».

I’m the one who downvoted when your answer included parsing ls . At the same time I had posted a link on why this is a bad idea on another answer that got deleted since. At the time there were only 2 answers and the reason seemed obvious. I’m glad you pointed out the ambiguity, and here’s my explanation. For the record, I turned my downvote in an upvote because I agree with your current answer.

@rahmu Giving an explanation for your downvote in another question is not helpful to me (and only obvious to you). Also, while this is a better solution, the solution I had posted before worked just fine (yes, I am familiar with your «don’t use ls» link — but it doesn’t apply blindly in all cases). If you have a counter example for why my initial solution wouldn’t have worked, I’d love to know it (so that I can actually learn something from this), otherwise you are just posting a link and not offering anything constructive.

Consider there’s a \n in a directory name you cannot use cut to retrieve it (which is what you were doing when I downvoted you). There are few cases where ls doesn’t behave dangerously and it’s almost always safer to use another way.

Источник

How can I see folders from terminal?

list directories themselves, not their contents. To explain this, consider what happens if we type ls */ . ls goes one layer down, into each subdirectory, and lists all the files in each of those sequentially

*/ is known as a «glob» in UNIX. (see Wikipedia for more details). But basically, it means «any file name ending in a forward slash.» In UNIX, directories are really just files, fundamentally. But they are specially named ending in a forward slash so the operating system knows they are directories (or folders, in everyday-person-speak). And the asterisk * is technically a wildcard standing for «any string of characters.»

Читайте также:  Virtual во linux wine

What is a glob?

This paragraph will not pertain specifically to your question, but if you’ve never read about this, it’ll be good to see it. Globs are different from Regular Expressions, as (partially) explained in What is the difference between Regular Expressions and Globbing? There have been whole books written on regular expressions, but tl;dr there are a bunch of different ways to encode pattern-matching expressions.

How to show hidden folders as well? ls -d .*/ shows only hidden folders. How to view BOTH hidden and non-hidden folders? I can only think of ls -d */ .*/ Anything better?

As I am a very inexperienced user I love this website. It tells you all you want to know about bash commands, in some cases it even gives you examples. Very useful.

  • ls to list the files
  • ls -a to include hidden files
  • ls -l for a long listing format
  • .

lists one directory per line.

If you want to be able to distinguish folders from files easily, use something like ls -alhF . I usually define l as an alias for that, ie. I put the line alias l=’ls -alhF’ in my .bashrc .

include hidden files ‘-a’ grep ^d get start with ‘d’ wich means directory when name starts with dot directory is hidden

to list recursively see this

find ./ -type d | less ./ . means starts find in current folder -type . indicates the type to be searched d . means directory | . redirects the command less . enables paging using the keyboard arrows and leave with q 
$ ls Desktop Downloads hadoop Pictures Templates Documents examples.desktop Music Public Videos 
$ ls ~ /usr /home/hadoop1: Desktop Downloads hadoop Pictures Templates Documents examples.desktop Music Public Videos /usr: bin games include lib local locale sbin share src 
$ ls -l total 48 drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Desktop drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Documents drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Downloads -rw-r--r-- 1 hadoop1 hadoop1 8980 Jul 1 2017 examples.desktop drwxr-xr-x 10 hadoop1 hadoop1 4096 Jul 1 2017 hadoop drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Music drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Pictures drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Public drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Templates drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Videos 

Источник

Оцените статью
Adblock
detector