30

In Linux, is there an equivalent to Dir /s /a /b where the full path and filename is listed? I'm new to Linux, and without a GUI, I want to get an idea of the structure of what's on the hard disk.

35

Use the find command. By default it will recursively list every file and folder descending from your current directory, with the full (relative) path.

If you want the full path, use: find "$(pwd)".

  • If you want to restrict it to files or folders only, use find -type f or find -type d, respectively.
  • If you want it to stop at a certain directory depth, use find -maxdepth 2, for example.

Read Finding Files for an extensive manual on GNU find, which is the default on Linux.

6
  • Thank you very much. Can you briefly describe what $(pwd) is actually instructing? Jan 3 '13 at 12:29
  • pwd is your working directory. Type it into console and you'll see where you are (useful if your prompt doesn't tell you).
    – mcalex
    Jan 3 '13 at 12:41
  • 4
    @LukePuplett pwd will print your current working directory. If you enclose a command with $(), its output (i.e. your working directory) will be substituted before the outer command is run. So find will actually not see find $(pwd), but find /home/luke/, for example. This is called command substitution.
    – slhck
    Jan 3 '13 at 12:41
  • You probably meant the second example in the first bullet point to be find -type d (d for directory). At the moment the two are identical. (And I don't have the rep to make "trivial" edits here at SU.)
    – user
    Jan 3 '13 at 12:56
  • @MichaelKjörling Indeed. Thank you for pointing out that typo.
    – slhck
    Jan 3 '13 at 12:57
6
$ pwd
/home/victoria

$ find $(pwd) -maxdepth 1 -type f -not -path '*/\.*' | sort
/home/victoria/new
/home/victoria/new1
/home/victoria/new2
/home/victoria/new3
/home/victoria/new3.md
/home/victoria/new.md
/home/victoria/package.json
/home/victoria/Untitled Document 1
/home/victoria/Untitled Document 2

$ find . -maxdepth 1 -type f -not -path '*/\.*' | sed 's/^\.\///g' | sort
new
new1
new2
new3
new3.md
new.md
package.json
Untitled Document 1
Untitled Document 2

Notes:

  • . : current folder
  • remove -maxdepth 1 to search recursively
  • -type f : find files, not directories (d)
  • -not -path '*/\.*' : do not return .hidden_files
  • sed 's/^\.\///g' : remove the prepended ./ from the result list
5

For completeness, the ls -lR / command will list the name of each file, the file type, file mode bits, number of hard links, owner name, group name, size, and timestamp of every file (that you have permission to access) from the root directory down. (l is for long list ie all that info, R is to recurse through directories, / starts at the root of the filesystem.)

There are a number of params to make the output info closer to dir /S /A, but I have to admit I can't work out how to translate the /B.

For useful info, I would try: ls -lAFGR --si /

where

  • l = long list (as mentioned above)
  • A = almost all files (doesn't include . and .. in each dir, but does show all hidden files)
  • F = show file indicator, (one of * for exe files, / for directories, @ for symbolic links, | for FIFOs, = for sockets, and > for doors)
  • G = don't show group info (remove this if you want to see it)
  • R = recursively list directories (subdirectories) and
  • --si = show the file size in human readable eg 1M format (where 1M = 1000B)

ls can provide an easier to read synopsis of directories and files within those directories, as find's output can be difficult to scan when files are contained within really long directory structures (spanning multiple lines). The corollary is that each file is listed on its own (ie without directory path information) and you may have to go back a couple of pages/screens to find the directories a particular file is located in.

Also, find doesn't contain the /A information in the DIR command. I've suggested a number of attributes in the command I've shown (that coincidentally show the extra usefulness that you get from linux over a certain proprietary system), but if you read the man and info pages on ls, you will be able to see what to include or not.

3
  • Thanks for this extra detail. For all the switches, its the non default human-readable one which makes me laugh. Jan 3 '13 at 17:31
  • lol, why would you want to see file sizes in anything but bytes?
    – mcalex
    Jan 3 '13 at 17:37
  • Why would you want to parse the ls output? ;P (Okay, I'll stop now.) But seriously, @LukePuplett, it's been the default for *nix systems since non-human readable file sizes are easier to calculate with.
    – slhck
    Jan 3 '13 at 17:46
4

Most answers here mention find or just ls or ll, and pwd but with the use of find.

here it is with the use of ll

plaj@her-dta-01:~> ll -d $PWD/*
drwxr-xr-x 2 plaj users 4096 Jul  3 10:27 /home/plaj/bin
-rw-r--r-- 1 plaj users  203 Aug 19 17:21 /home/plaj/file.json
drwxr-xr-x 5 plaj users 4096 Aug 12 08:06 /home/plaj/file2
drwxr-xr-x 6 plaj users 4096 Sep  2 09:37 /home/plaj/file3
drwxr-xr-x 5 plaj users 4096 Aug 12 15:15 /home/plaj/file4
-rw-r--r-- 1 plaj users  566 Sep  2 11:10 /home/plaj/etcfile.log
2
1

I had this same question today. I found a command called "realpath" here's an example:

$ realpath ~/.bashrc
/home/harleygolfguy/.bashrc
2
  • Funny I've used realpath (3) in my code and I knew there was a realpath command but didn't even think of this. Note however if it's a symlink you'll get what it resolves to, not the path of the link.
    – Liam
    Feb 6 at 20:07
  • Thanks @Liam ... that's good to know about the symlink. Feb 10 at 20:27
0

find $(pwd) -maxdepth 3 -type f -not -path '/\ml.*' | sort > ~/masterthesis/preprocessing/data/ERA5_foehn/fnames/B_full.txt

1
  • 1
    Welcome to Super User! Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
    – MMM
    Mar 19 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.