Include all directories linux

Linux / UNIX List Just Directories Or Directory Names

You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too. In this quick tutorial you will learn how to list only directories in Linux or UNIX.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux, macOS, or Unix terminal
Est. reading time 5 minutes

Display or list all directories in Unix

Type the following command:
$ ls -l | grep `^d’
$ ls -l | egrep `^d’
Or better try the following ls command only to list directories for the current directory:
$ ls -d */
Sample outputs:

Fig.01: List Directories in Unix and Linux Systems

Linux list only directories using ls command

Run the following ls command:
ls -d */

Listing only directories using ls command in Linux or Unix-like systems

Linux Display or list only files

Type the following command to display list only files in Linux or Unix:
$ ls -l | egrep -v ‘^d’
$ ls -l | egrep -v ‘^d’
The grep command is used to searches input. It will filter out directories name by matching first character ‘ d ‘. To reverse effect i.e. just to display files you need to pass the -v option. It invert the sense of matching, to select non-matching lines.

Task: Create bash shell aliases to save time

You can create two aliases as follows to list only directories and files.
alias lf=»ls -l | egrep -v ‘^d'»
alias ldir=’ls -d */’
##alias ldir=»ls -l | egrep ‘^d'»
Put above two aliases in your bash shell startup file:
$ cd
$ vi .bash_profile

Append two lines:
alias lf=»ls -l | egrep -v ‘^d'»
alias ldir=’ls -d */’
#alias ldir=»ls -l | egrep ‘^d'»
Save and close the file in vim. Now just type lf – to list files. Again run ldir to list directories only:
$ cd /etc
$ ldir
Sample outputs:

List directory names only:
$ cd /etc
$ ldir

Sample outputs:

Use find command to list either files or directories on Linux

The find command can be used as follows to list all directories in /nas, enter:

Pass the -maxdepth 0 to limit listing to the starting-points i.e. the current working directory only:
find /path/to/dir -maxdepth 1 -type d
find . -maxdepth 1 -type d
find . -maxdepth 1 -type d -ls

Listing only directories using the find command in Linux

Putting it all together

Say you want to find all directories ending with .bak extension and delete it, run the following find command in the current directory:
find . -type d -iname «.bak» -delete
Verify it:
find . -type d -iname «.bak» -ls
The following shell script does two things for Apache/Nginx/Lighttpd Webroot such as /webroot/:

  1. First, finds all files and directories and set permission to read-only for security reasons.
  2. Second, it allows our web server to read files regardless of permission so that we don’t get an HTTP/403 error.

In other words, all write permissions are removed from Webroot. The server/web-app can only read files but can not alter any files or upload any files. It helps reduces attack surfaces provided that you configure the rest of the server and web application firewall correctly.

Источник

c++ list all directories and subdirectories within in (LINUX ) [closed]

Want to improve this question? Update the question so it’s on-topic for Stack Overflow.

Closed 8 years ago .

I am new in c ++ . could someone please give me some code of how to get all directories and all it’s subdirectories RECURSIVELY in LINUX. i haven’t found anything on internet that could help me ( or code that works.) I need to get all files withing folder and it;s subfolder.

IN UBUNTU I don’t have getfiles, directories.

6 Answers 6

Try this on Linux:

Recursively apply the same algorithm you applied to the top-level directory.

Recursion is unnecessary. There is a facility on Linux to do this iteratively.

The ftw() function calls the supplied callback for every file and directory in the given tree.

In addition of Dmitri’s answer, you might be interested in using the nftw library function which «does the recursion for you»

Use nftw . It provides various options to fine-tune the directory traversal.

That page also shows an example.

The answers on listing directories are only the first part of the answer but i didnt see anyone answering the recursive part. To recursively do anything you have to create a subroutine that «calls itself» — note that you should take care when dealing with symbolic links especially in cases like /exports when using nfs, that could lead to circular recursion and lock you in an infinte loop! Basically:

this isn’t real code, its pseudo-code to try to help you get a better idea of how the recursion works without confusing you with language this idea can be applied in any language that has some sort of call-return mechanism which these days is just about every language i can think of

notice how if the entries do not contain any real directories, the function doesn’t call itself? this is important because this is how infinite recursion is avoided. Be warned however you might want to make it so this operation can be cancelled, larger filesystems these days can take a lot of time to process. The way I usually do it is to start another thread and do the search in the background, and have the background thread check for a cancellation flag in case the user wants to stop the operation, and so it can post information about how much time is left, percentage complete, etc ,etc. its a bit crude but this should get anyone interested in this type of thing going in the right direction. And remember to ALWAYS properly check for errors and place exception handling, this is something that I see new programmers skip ALL the time.

Источник

linux include all directories

how would I type a file path in ubuntu terminal to include all files in all sub-directories?

If I had a main directory called «books» but had a ton of subdirectories with all sorts of different names containing files, how would I type a path to include all files in all subdirectories?

3 Answers 3

From within the books top directory, you can use the command:

Then, if you wanted to, say run each file through cat, you could use the xargs command:

find . -type f | xargs cat

For more info, use commands:

For example, assuming i’m in the parent directory of ‘books’:

EDIT:

Actually, to list all the tree recursively you should use:

It is unclear what you actually want . Probably you will get a better solution to your problem, if you ask directly for it, not for one other problem you’ve come accross trying to circumvent the original problem.

do you mean something like the following?

where the first * expands for all subdirectories and the second * for all contained files ?

I have chosen the file command arbitrarily. You can choose whatever command you want to run on the files you get shell-expanded. Also note that directories will also be included (if not excluded by name, e.g. *.png or *.txt ). The wildcard * is not exactly the file path to include all files in all subdirectories but it expands to all files (or directories) matching the wildcard expression as a list, e.g. file1 file2 file3 file4 . See also this tutorial on shell expansion.

Note that there may be easy solutions to related problems. Like to copy all files in all subdirectories ( cp -a for example, see man cp ).

I also like find very much. It’s quite easy to generate more flexible search patterns in combination with grep . To provide a random example:

Источник

tar: add all files and directories in current directory INCLUDING .svn and so on

I try to tar.gz a directory and use

The resulting tar includes .svn directories in subdirs but NOT in the current directory (as * gets expanded to only ‘visible’ files before it is passed to tar

tar -czf workspace.tar.gz . instead but then I am getting an error because ‘.’ has changed while reading:

Is there a trick so that * matches all files (including dot-prefixed) in a directory?

(using bash on Linux SLES-11 (2.6.27.19)

16 Answers 16

Don’t create the tar file in the directory you are packing up:

does the trick, except it will extract the files all over the current directory when you unpack. Better to do:

or, if you don’t know the name of the directory you were in:

(This assumes that you didn’t follow symlinks to get to where you are and that the shell doesn’t try to second guess you by jumping backwards through a symlink — bash is not trustworthy in this respect. If you have to worry about that, use cd -P .. to do a physical change directory. Stupid that it is not the default behaviour in my view — confusing, at least, for those for whom cd .. never had any alternative meaning.)

One comment in the discussion says:

I [. ] need to exclude the top directory and I [. ] need to place the tar in the base directory.

The first part of the comment does not make much sense — if the tar file contains the current directory, it won’t be created when you extract file from that archive because, by definition, the current directory already exists (except in very weird circumstances).

The second part of the comment can be dealt with in one of two ways:

Источник

How to show recursive directory listing on Linux or Unix

I am a new Linux system user. How do I see a recursive directory listing on macOS Unix system? In Linux, how can I get a recursive directory listing?

Introduction – If you like to receive the list, all directories and files recursively try the following commands.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux and Unix-like OS
Est. reading time 3 minutes

What is a recursive listing of files?

Recursive means that Linux or Unix command works with the contains of directories, and if a directory has subdirectories and files, the command works on those files too (recursively). Say you have a directory structure as follows:
tree dir1

From the above outputs, it is clear that running the tree dir1 gives a list of dir1 directory and its subdirectories and files. The base directory is dir1. Then you have all the child directroies. All all the child directories have additional files and directories (say grand directories), and so on. You can use various Linux commands going through each directory recursively until it hits the end of the directory tree. At that point Linux commands come back up to a branch in the tree a does the same thing for any sub-directories if any.

How to get a recursive directory listing in Linux or Unix

Try any one of the following command:

  1. ls -R : Use the ls command to get recursive directory listing on Linux
  2. find /dir/ -print : Run the find command to see recursive directory listing in Linux
  3. du -a . : Execute the du command to view recursive directory listing on Unix

Let us see some examples to get a recursive directory listing in Unix or Linux systems.

Linux recursive directory listing command

Type the following command:
ls -R
ls -R /tmp/dir1

Linux recursive directory listing using ls -R command.

Unix recursive directory listing command

Since, not all versions of Linux, macOS, *BSD, and Unix-like system have -R option for the ls command. Try to use find command:
find . -print
find /tmp/dir1 -print
find /tmp/dir1/ -print -ls

Recursive directory listing in Linux or Unix using the find command

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How to list all files recursively in a directory

Our final example uses the du command as follows:
du -a .
du -a /tmp/dir1/
You can also use the tree command as follows:
tree .
tree /tmp/dir1/

Recursively working with files

It is possible to run command recursively on files. The syntax is:
my-command-here $(find /dir/ -name ‘pattern’ -print)
rm -i $(find /home/nixcraft/ -name ‘*.bak’ -print)
Of course, your can run command using find itself:
find /dir1/ -name ‘pattern’ -print -exec command ;
find /dir1/ -name ‘pattern’ -print -exec command <> ;
find /dir/2/foo/bar -name «*.pl» -exec rm -rivf <> \;
find /dir1/ -type f -name «*.doc» -exec rm -fiv <> \;
## find file recursively and delete them ##
find /dir1/ -name ‘pattern’ -print -delete
See “Linux / Unix: Find And Remove Files With One Command On Fly” for more info.

Conclusion

You learned how to list all files recursively in a directory under Linux, macOS, *BSD and Unix-like operating system using the ls, du, and find commands.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Читайте также:  Windows bulletin что это
Оцените статью