nomadnode.blogg.se

List directory contents that start with in linux
List directory contents that start with in linux





  1. #LIST DIRECTORY CONTENTS THAT START WITH IN LINUX HOW TO#
  2. #LIST DIRECTORY CONTENTS THAT START WITH IN LINUX MAC OS X#
  3. #LIST DIRECTORY CONTENTS THAT START WITH IN LINUX MAC#

a - look for hidden files and directories as well.You can modify it to show only directories and only at the current level. If your aim is to list only the directories, you may also use the tree command.īy default, the tree command gives you the complete directory structure. Use tree command to list only directories maxdepth 1 -type dĪs you can see in the output above, it also shows the hidden directory. With maxdepth 1 you ask the find command to keep the search at the current level only (and not go inside the subdirectories). With type d, you ask the find command to only look for directories. I hope you are familiar with the find command.

#LIST DIRECTORY CONTENTS THAT START WITH IN LINUX HOW TO#

Here's how to use the find command to list only the subdirectories: find directory_path -maxdepth 1 -type d Use find command to list only directories You can use grep to filter the contents that start with d: ls -l | grep '^d'īut this gives you a lot more fields than just the directory names: Documents]$ ls -l | grep '^d' If you long list the contents, you can identify the directories because start with d. You can always rely on the good old grep command for filtering the output for specific content. */ to display hidden directories, but it only displays hidden directories. 1 abhishek abhishek 44 Nov 7 18:22 Documents/my_dir/ĭid you notice that it doesn't list the hidden directory? That's one shortcoming of this method. 1 abhishek abhishek 16 Nov 7 18:22 Documents/another_dir/ĭrwxrwxr-x. Here's an example where I move out of the Documents directory and then list only the directories inside Documents directory: ~]$ ls -ld Documents/*/ĭrwxrwxr-x. In this situation, you can use */ at the end of the path of the directory with ls -d: ls -d Path/To/Dir/*/ What if you are not in the same directory? The above command works in the current directory.

list directory contents that start with in linux list directory contents that start with in linux list directory contents that start with in linux

List only subdirectories in a specific directory 1 abhishek abhishek 44 Nov 7 18:22 my_dir 1 abhishek abhishek 16 Nov 7 18:22 another_dirĭrwxrwxr-x. If you do not want the trailing slash (/) at the end of the directory names, you can use the cut command to cut it out: Documents]$ ls -ld */ | cut -f1 -d'/'ĭrwxrwxr-x. 1 abhishek abhishek 44 Nov 7 18:22 my_dir/ 1 abhishek abhishek 16 Nov 7 18:22 another_dir/ĭrwxrwxr-x. You may combine it with long listing option -l and most other options: Documents]$ ls -ld */ĭrwxrwxr-x. This picture depicts the difference pretty well. With *, you list all the content (including contents of the subdirectories) and the / restricts the pattern to directories. The -d option list directories not its contents (which includes file, directories etc). Why */? Because without it, ls -d will only return the directory name. Here's the output it shows: Documents]$ ls -d */ To list only the subdirectories, use the -d option with ls command like this: ls -d */ It is always good to do it with the familiar ls command because this is the command you use for displaying the content of a directory. Here's the content of the directory I am going to use in the examples here: Use ls command to list directories only Listing only the directories is no different:ĭon't worry. Like anything in Linux, there are several ways to accomplish the same task.

list directory contents that start with in linux

As you can see from the first column (beginning with " drwx"), each row that is printed is really a directory.The ls command in Linux is used for listing the contents of any directory.īy default, it lists all the contents, be it a file or a directory or a link or a named pipe.īut what if you want to list only the directories? How do you that? I've trimmed a few directories off the list to keep it short, but you get the idea.

#LIST DIRECTORY CONTENTS THAT START WITH IN LINUX MAC#

In the home directory on my Mac the output from this command look like this:ĭrwxrwxr-t 6 root admin 204 Jul 2 18:13. This works because directories on Unix/Linux systems are always listed with the letter 'd' in the first column. This ls command works by first generating a long list (the -l option) of all files ( -a) in the current directory, then uses the grep command to only show those lines that begin with the letter 'd' (the ^d portion of the command, where the ^ portion in particular means "line begins with"). The answer is yes, all you have to do is this:

#LIST DIRECTORY CONTENTS THAT START WITH IN LINUX MAC OS X#

Does your business have a small Scala or Flutter side project? You can now hire Alvin Alexander.Ĭontact me at (al) at valleyprogramming (dot) com for details.Ī friend asked the other day if there is any easy way to list all the sub-directories of the current directory on a Unix, Linux, or Mac OS X system.







List directory contents that start with in linux