Hot answers tagged ls
59
The a* and *a* syntax is implemented by the shell, not by the ls command.
When you type
ls a*
at your shell prompt, the shell expands a* to a list of all files in the current directory whose names start with a. For example, it might expand a* to the sequence a1 a2 a3, and pass those as arguments to ls. The ls command itself never sees the * character; it ...
42
When you run ls without arguments, it will just open a directory, read all the contents, sort them and print them out.
When you run ls *, first the shell expands *, which is effectively the same as what the simple ls did, builds an argument vector with all the files in the current directory and calls ls. ls then has to process that argument vector and for ...
31
A good way to inspect what a command is:
type l
If it's a program or a script, it will give you its location, if it is an alias, it will tell you what it's aliased to, if it's a function, it will print the funciton; otherwise, it will tell you if it is a built-in or a keyword.
Examples:
$ type find
find is /usr/bin/find
$ type connecthome
connecthome is ...
27
It indicates the file has extended attributes. You can use the xattr command-line utility to view and modify them:
xattr --list filename
xattr --set propname propvalue filename
xattr --delete propname filename
26
I know there is already a selected answer, but you can get the requested behavior with just ls:
ls -ld */
This will list all the directories in the current working directory where it is run. To get all the subdirectories of some other folder, just try:
ls -ld /path/to/directory/*/
Note that the -l is optional.
26
A shell assignment is a single word, with no space after the equal sign. So what you wrote assigns an empty value to thefile; furthermore, since the assignment is grouped with a command, it makes thefile an environment variable and the assignment is local to that particular command, i.e. only the call to ls sees the assigned value.
You want to capture the ...
25
The command ls defaults to ls .: List all entries in the current directory.
The command ls * means 'run ls on the expansion of the * shell pattern'
The * pattern is processed by the shell, and expands to all entries in the current directory, except those that start with a .. It will go one level deep.
The interpretation of double or triple * patterns ...
20
When you pipe the output, ls acts differently.
This fact is hidden away in the info documentation:
If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the output is listed one per line and control characters are output as-is.
To prove it, try running
ls
and ...
20
See Keith Thompson’s answer; but to explain why ls --directory a* shows files and directories: The --directory option does not suppress non-directory files. Instead, it lists the directories as such, while it would otherwise list their content. Example:
$ mkdir foo
$ touch foo/bar
$ ls foo
bar
$ ls --directory foo
foo
19
You can use the find command to find all files that have been modified after a certain number of days.
For example, to find all files in the current directory that have been modified since yesterday (24 hours ago) use:
find . -maxdepth 1 -mtime -1 -ls
18
The direct equivalent is
find . -name <filename>
If you only want to list files called <filename>, and not directories, do this:
find . -name <filename> -type f
If you want to use wildcards, you need to put quotes around it, e.g.
find . -name "*.txt"
otherwise the shell will expand it.
As others have pointed out, you can also do:
...
18
It will happen if you have sparse files:
$ mkdir test; cd test
$ truncate -s 1000000000 file-with-zeroes
$ ls -l
total 0
-rw-r--r-- 1 gim gim 1000000000 03-08 22:18 file-with-zeroes
A sparse file is a file which has not been populated with filesystem blocks (or only partially). When you read a non-populated zone of a sparse file you will obtain zeros. ...
16
Most unices do not have a concept of file creation time. You can't make ls print it because the information is not recorded. If you need creation time, use a version control system: define creation time as the check-in time.
If your unix variant has a creation time, look at its documentation. For example, on Mac OS X (the only example I know of¹), use ls ...
16
The ls program uses isatty() to know whether fd 1 is a tty or something else (pipe, file, etc…). From man 3 isatty:
int isatty(int fd);
DESCRIPTION
The isatty() function tests whether fd is an open file descriptor
referring to a terminal
Updade: Line 1538 in ls.c from coreutils (git revision 43a987e1):
if (isatty ...
16
If you run ls normally, it will just show the list of files without needing to run stat(2) on any of them. In other words, it doesn't access the FILES themselves, but only the directory that contains the files.
If you add in the --color option, or use other ls options that need to examine the files themselves, then ls will need to stat(2) those files.
...
16
ls -l --block-size=M will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB.
If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use --block-size=MB instead.
If you don't want the M suffix attached to the file size, you can use something like --block-size=1M. Thanks ...
15
This is actually done by your shell, not by ls.
In bash, you'd use:
shopt -s nocaseglob
and then run your command.
Or in zsh:
unsetopt CASE_GLOB
and then your command.
You might want to put that into .bashrc or .zshrc, respectively.
Alternatively, with zsh:
setopt extendedglob
ls -d -- (#i)*abc*
(that is turn case insensitive globbing on on a ...
15
The following seems to work for me
grep --color -E -- "$(ls -rtl | tail -n3)|$" <(ls -l)
It uses grep with highlight on input ls -l and uses a regular expression to search for either of the inputs for the three oldest command. It also search for the end-of-line $ in order to print the whole file.
You can also put it in a function, such that you can ...
14
stat from GNU coreutils can do this:
stat -c '%U' /path/of/file/or/directory
Unfortunately, there are a number of versions of stat, and there's not a lot of consistency in their syntax. For example, on FreeBSD, it would be
stat -f '%Su' /path/of/file/or/directory
If portability is a concern, you're probably better off using Gilles's suggestion of ...
13
You probably did a copy that preserved the original group and owner of these files. Within linux internally the owner and group is basically just an id (in your case, the number 515). This id is then mapped on a group and user name listed in /etc/passwd or /etc/group. You will see that in those files, you can find the name of the user and also the id used ...
13
Find supports -o
find \! '(' -name '*.txt' -o -name '*.pdf' ')'
You need the parenthesis to make the precedence right. Find does a lot of stuff; I suggest reading through its manpage.
You can also do an or in grep (but really, you should not parse the output of ls)
ls | egrep -v '\.(txt|pdf)$' | column
12
find -maxdepth 1
find -maxdepth 1 -ls
less TAB TAB
finds -ls switch is independent of /bin/ls and has its own format, and displays detail information:
127432 0 drwxr-xr-x 2 stefan stefan 48 Apr 8 22:51 ./temp/falsch/.hiddenfalsch
127447 0 lrwxrwxrwx 1 stefan stefan 9 Apr 8 22:51 ./temp/falsch/linkfalsch -> ...
11
I think that your best bet is the find command. If you want just the files and not the directories in your list, something like this:
find directory/ -type f -print > textfile
The find command will recursively list the files. (If you want the directories listed too, remove the -type f). The > textfile redirects stdout to a file named textfile with ...
11
Got GNU?
The gnu version of ls has --group-directories-first. And cp has -t.
No GNU?
On systems that don't have gnu's ls, your best bet is two successive calls to find with -maxdepth n/-mindepth n and -type t with the appropriate options.
find . -maxdepth 1 -mindepth 1 -type d
find . -maxdepth 1 -mindepth 1 \! -type d
For copying files, with the target ...
11
It's because the caret is often used to signify the ctrl key having been pressed, or that it's otherwise a control character.
The key sequence that you actually typed was this:
cp filename.xsl .ctrl+Vbackspace~Enter
You were presumably trying to copy the file to your home directory (~). You can repeat this by typing ctrl+Vbackspace. You'll see ^? printed ...
11
Note: edited after @StephaneChazelas comment
The first number of the ls -l output after the permission block is the number of hard links.
It is the same value as the one returned by the stat command in "Links".
This number is the hardlink count of the file, when referring to
a file, or the number of contained directory entries, when referring
to a ...
11
Shell file name globbing and regular expressions use some of the same characters, and they have similar purposes, but you're right, they aren't compatible. File name globbing is a much less powerful system.
In file name globbing:
* means "zero or more characters"
? means "any single character"
Square brackets ([]) appear to work just like regexes on the ...
11
When ls is executed it parses various options. It also detect if output is a tty or not by isatty().
ls.c:
code
case LS_LS:
/* This is for the `ls' program. */
if (isatty (STDOUT_FILENO))
{
format = many_per_line;
/* See description of qmark_funny_chars, above. */
qmark_funny_chars = true;
}
else
{
format = ...
10
The answer will depend more on what you intend to do with the output than on what you are looking for. If you just want to see a list for visual reference at the terminal, your first solution is actually pretty nice. If you want to process the output you should consider using another method.
One of the most robust ways to get a list to feed into another ...
10
If you have GNU utilities (or at least a set that can deal with zero-terminated lines) available, another answer has a great method:
find . -maxdepth 1 -print0 | sort -z | uniq -diz
Note: the output will have zero-terminated strings; the tool you use to further process it should be able to handle that.
In the absence of tools that deal with ...
Only top voted, non community-wiki answers of a minimum length are eligible

