55

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.

7 Answers 7

64

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 the relative path, use:

find .

Here, $PWD is a variable containing the current working directory.

Some options:

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

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

8
  • Thank you very much. Can you briefly describe what $(pwd) is actually instructing? Jan 3, 2013 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, 2013 at 12:41
  • 5
    @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, 2013 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, 2013 at 12:56
  • 2
    Why not find "$PWD"?
    – Zombo
    Apr 23, 2016 at 18:24
8

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

$ realpath ~/.bashrc
/home/harleygolfguy/.bashrc
3
  • 1
    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, 2021 at 20:07
  • Thanks @Liam ... that's good to know about the symlink. Feb 10, 2021 at 20:27
  • and how on earth this answers the question? realpath doesn't list files recursively like dir /b /s at all
    – phuclv
    Jan 27, 2023 at 6:41
8

Linux solution:

$ 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
7
  • This didn't work for me if there are spaces in the directory names (OSX). Jan 27, 2023 at 5:41
  • 1
    @JarrodSmith OS X isn't Linux.
    – Marco
    Jan 27, 2023 at 5:52
  • @Marco it's irrelevant here because it's the shell that's important. In all common POSIX shells this won't work for names with spaces due to the word splitting and you must use "$PWD" like in slhck's answer. "$(pwd)" also works but it's far worse because it requires another subshell to be spawned
    – phuclv
    Jan 27, 2023 at 6:39
  • @phuclv You're right. Still, this question is tagged linux so readers should not expect it to work on, lets say, macOS or OS X. There are very real and subtle differences between the two.
    – Marco
    Jan 27, 2023 at 6:51
  • @Marco but on Linux it still won't work if there are spaces in the directory names. That's a very real thing that anyone must know
    – phuclv
    Jan 27, 2023 at 7:39
6

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
3
  • 1
    Yes ls also works like ls -d $PWD/* for more detail rapidtables.com/code/linux/ls/ls-full-path.html
    – Gagan
    Apr 24, 2020 at 9:10
  • 2
    If there are spaces in your path, you will need quotes ll -d "$PWD"/*
    – Liam
    Feb 6, 2021 at 17:30
  • 3
    this is wrong, it won't recurse into subdirectories more than 1 level deep
    – phuclv
    Jan 27, 2023 at 6:40
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, 2013 at 17:31
  • lol, why would you want to see file sizes in anything but bytes?
    – mcalex
    Jan 3, 2013 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, 2013 at 17:46
1

find s/ a/ b/ -exec realpath {} \;

NOTE: you should be in the dir where s, a and b are present as nested dirs and you should have realpath unix utility (or command installed), it is a useful command. It prints the absolute path of the given file which we are feeding to it via -exec.

-2
find $(pwd) -maxdepth 3 -type f -not -path '*/\ml*.*' | sort > ~/masterthesis/preprocessing/data/ERA5_foehn/fnames/B_full.txt
2
  • 4
    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, 2021 at 9:01
  • again this suffers from the same issue as other answers: it won't work for names with spaces
    – phuclv
    Jan 27, 2023 at 6:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .