Linux list all file names

Recursively list all files in a directory including files in symlink directories

Suppose I have a directory /dir inside which there are 3 symlinks to other directories /dir/dir11 , /dir/dir12 , and /dir/dir13 . I want to list all the files in dir including the ones in dir11 , dir12 and dir13 . To be more generic, I want to list all files including the ones in the directories which are symlinks. find . , ls -R , etc stop at the symlink without navigating into them to list further.

8 Answers 8

The -L option to ls will accomplish what you want. It dereferences symbolic links.

You can also accomplish this with

The -follow option directs find to follow symbolic links to directories.

as -follow has been deprecated.

@pjz: is there a cross-reference for ‘-follow deprecated; use -L’? Somewhat to my considerable surprise, I did find ‘-L’ and ‘-H’ listed in the POSIX / SUS standard at opengroup.org/onlinepubs/009695399/toc.htm, and even more to my surprise no ‘-follow’, so I answered my own question.

this didn’t work for me. first nothing happened then i tried with -follow — and it said it couln’t find the folder ollow

Your answer is good, but I want to follow only file paths(full path to file) and not directory path, how can I do it ?

How about tree? tree -l will follow symlinks.

Disclaimer: I wrote this package.

It is a nice interface, but sometimes tree is not available and you cannot add it to the system you are trying to explore.

find /dir -type f -follow -print 

-type f means it will display real files (not symlinks)

-follow means it will follow your directory symlinks

-print will cause it to display the filenames.

If you want a ls type display, you can do the following

find /dir -type f -follow -print|xargs ls -l 
 -L, --dereference when showing file information for a symbolic link, show informa‐ tion for the file the link references rather than for the link itself 

If you find you want to only follow a few symbolic links (like maybe just the toplevel ones you mentioned), you should look at the -H option, which only follows symlinks that you pass to it on the commandline.

Читайте также:  Tl wn822n arch linux

I knew tree was an appropriate, but I didn’t have tree installed. So, I got a pretty close alternate here

find -L /var/www/ -type l # man find 
-L Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the 

properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched.

Источник

Make a list of filenames [duplicate]

I’m looking for an easy way to build a list in a txt file of the filenames inside a given directory. Filenames only preferred, though I could trim other fields if necessary.

The post named in comment above wanted subfolders, path, size, and creation (or last modified) date . The original question for this post wanted none of that, just names.

3 Answers 3

This command should be helpful:

find -maxdepth 1 -type f ! -name flist.txt -printf "%P\n" > flist.txt 
  • -maxdepth : Don’t search for files below folders of one level down.
  • type f : Search for only files
  • -printf «%P\n» : Print the names only and on separate lines
  • > flist.txt : Store those names (using output redirection) in a file to be created on the fly called `flist.txt
  • ! -name flist.txt : Skips the name of the output file from the generated list

The ls (list) command executed in a terminal window is the key, and its man page lists options.

Let’s say you are in your home directory, and you want a list of all the files in directory /x/y . The command would be

for the -b option lists all files even with spaces, -1 (number one, not letter l) option of ls will list with one line per file name, and -A will make sure hidden files are shown, but not directories.

In terminal, change directory to the required folder, then use the command:

Читайте также:  Linux for windows gui

This will redirect a list of the contents of the folder to the text file files.txt . You didn’t indicate what form the contents of the folder takes. If there are sub-folders present, in addition to a group of files, the folder names will also be included. But, once you have the text version you can sort/edit it any way you desire.

ls omits “hidden” dot files (unless instructed otherwise) and mangles file names with some unusual characters. Don’t use it to list file names for anything but display to a human reader. -1

Many people don’t like this solution and downvote the answer — which is their prerogative. However, I would point out that the OP asked for an «easy way» to list files. Some of us have simple folder structures. We don’t have concerns with hidden files. And, I did realize that the simple command would include files.txt, but I did say that the result could be edited. Anyway, for what it’s worth, that’s the simple-minded way I do things.

Источник

How to list all filenames in the system [closed]

I want to perform checks on all files in the system. For that I need the full paths of all files in the system. My initial idea was to do something like this:

for file in $(sudo find / ); do if [ -d $file ]; then and so on. 

But then I read that it’s bad practice to process the output of find that way. What then is the correct way? (I tried things like ls -RF | grep «/$». That, however, only gives me the directory names but I need the full path of every file, directory etc. in the system.)

You seem a bit conflicted about what you’re looking for: title, initial find command, and the end of the body all point to «every file (and directory)», but your test -d and grep /$ indicate only directories. What are you actually trying to do?

The -d and the grep /$ were just examples — the ls -F adds a character to the filename also for file types other than directories and the -R lists recursively but only displays filenames, grouped by directories. Ultimately I need to list the full paths of all files in the system and invoke a variety of the conditional operators on them (-d, -s, -L, -p etc.) I was looking for an option to ls that does that but all I found was ls -RF.

Читайте также:  Linux менеджер базы данных

@Arjen If you have clarifications to your question, then update the question, don’t add information in comments.

if you extended your [ if -d .. example to show the various operators, it’d be clearer that you don’t need a simple find -type d

2 Answers 2

Use the -exec option of find. Your sample script would be written as:

sudo find / -type d -exec myprog <> \; 

The <> will be replaced by each file (directory in this case) found.

If you want to do different things on each entry depending on whether it’s a directory or regular file or whatever, you can put that logic into «myprog.sh» or just call find multiple times with different -type selections.

EDIT: For a single-instance script option, write myprog as:

#! /bin/bash - while IFS= read -rd '' FILE; do if [ -d "$FILE" ]; then . done 

(or replace -print0 with -exec printf ‘%s\0’ <> + if your find doesn’t support -print0 ).

@Kusalananda Like I said, the type d is to model their sample script (replacing the test -d). It was just an example. As I also said, if they want all, they can move that logic to their script.

Yes, thanks. Indeed I now use a myprog version that performs all the tests I need on $1. But now that program is invoked for every object found and that will take considerably longer. And the other problem is that myprog has no memory of what happened before. What if I just want to count the finds? As far as I understand I can’t just update an environment variable with a count. I could add a character to a disk file for every find and then do a wc -c < disk_file later. Feels a bit messy. Is there a better way to do that?

xargs is one way to pass many files to a single program. find / -print | xargs myprog Or just write myprog to read filenames from standard input and pipe directly to it.

Источник

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