How to access file start with period '.' in shell - linux

Special characters can be distinguished using backslash character \ . However, I want to treat full period as a normal character to operate on hidden folders. For example:
ls -lh .
It will list the current directory. However, I want to list all the hidden folders. I want it to be usable with du -h, so I know the disk space the hidden folders consumed.

Files and directories whose names start with . are "hidden" only in the sense that (a) ls ignores them by default and (b) wildcard expansion excludes them. In both cases, you can see dot files if you refer to them explicitly. * expands to all non-dot files; .* expands to all dot files.
(Other tools and commands may also treat them as hidden; for example, GUI file managers like Nautilus typically don't show dot files by default, but there's often an option to show them.)
ls -a overrides the special treatment of files whose names start with .. ls -A lists "hidden" files and folders, but excludes . (this directory) and .. (the parent directory); some versions of ls might not support -A.
The du command, as far as I know, doesn't treat dot files as hidden. du -h should show the entire directory tree starting at the current directory. (Try it in a small directory tree to make sure yours behaves this way.)
EDIT :
I've confirmed that at least the GNU coreutils version of du doesn't treat files or directories whose names start with . specially; nothing is hidden.
For example, this script:
#!/bin/sh
mkdir -p .dot/.dot .dot/nodot nodot/.dot nodot/nodot
du -h
produces this output on my system (the specific numbers depend on the nature of the file system, and are irrelevant to the current discussion):
4.0K ./.dot/.dot
4.0K ./.dot/nodot
12K ./.dot
4.0K ./nodot/.dot
4.0K ./nodot/nodot
12K ./nodot
32K .
Does that meet your requirements? If not, can you explain more clearly just what you're looking for? Do you want to list all directories, whether their names begin with . or not? Do you want to list only "hidden" directories? What output would you like for the directory structure created by the above script?

Wrong place for this question, but it's simple enough:
ls -lhd .*

what about ls -lh .* ? (This may answer you question)

Related

How to display full path of file using ls in linux with -R option in conjunction with grep

I'm trying to find a way to recursively look through a directory and display the full path of the files to the right of the files listed. For example, this is what happens when I search and list all files within the directory and subdirectories:
example here
With Grep, it simply shows the file name but not the full path, which I expected from the first picture. Is there anyway I can show the absolute path?
example here
I know adding -d option can provide this, but it only shows directories/files at that certain level of the directory and if I wanted to show all levels, I would have to keep track of how many levels there are within the directory and print each level one by one.
example here
If you use bash, you can use globstar to expand the paths from the start:
shopt -s globstar
ls -lhd path/**
Of course there's also
find path -ls

Group directories which are symbol links first with ls

I employed the predicate --group-directories-first to display the directories on the top.
However, it take effect singularly on the real directories but excluding the directories which are sym-links.
For example, in the Downloads directory, I make dozens of symlink-directories to facilitate the categories of freshly downloaded file.
How could group directories which are syslinks first?
You can use -L option to group directories with symlink first
ls -lL --group-directories-first

How I can hide original directory of linked folders in terminal while i use ”ls -la”

I often use ls -la command for view hidden files in terminal.
But I'm uncomfortable with the fact that it shows and links to directories of linked folders.
Can I hide this?
The -L option may be what you are looking for, although this also changes what metadata is shown. Without -L, you get permissions, size, etc for the symlink. With -L, the symlink's name alone is shown, but with the permissions, size, etc of the target.
ls -la /usr/bin/ | awk 'BEGIN{FS="->"}{print $1}'
then you can alias it , so don't have to type all of that every time

The output of ls with -aF option is not clear

When I try the command ls with -aF option inside any directory whether it's empty or not, I always got the following:
./ ../
so what does the output mean when I have these two options together with ls command?
When you use ls, you are reading a directory file, not actually looking in a directory. Every directory listing contains an entry for the present/current directory, as well as its parent directory, just as you would expect to also see listings for sub/child directories in any directory listing.
The -A option for ls merely tells ls to display ALL files, which includes the entries of ./ & ../ for present and parent. Note that these dots are merely a shorthand that the shell (bash) uses to represent file paths for those files. In other words, what "./" really means is say ~/Desktop if you were currently in the Desktop directory doing an ls. And "../" would mean "~/" which is just another symbolic shorthand to represent your user home directory, which is probably something like /Users/your_username on macOS (OS X), or /usr/your_username for various Linux distributions. Note that those paths could also be written with the forward slash appended at the end and would mean the same thing (e.g., /Users/your_username/ is the same as /Users/your_username because they are both references to other directories (directory files).
Use the -a option for ls if you don't want to see ./ & ../, but still want to see (other) hidden files.
Using the -F option causes ls to display appended characters to the file types based on the file type. This is why directories are displayed with the forward slash appended at the end, and executables are displayed as executable* (with the asterisk appended), and regular files have no appendage (e.g., .txt, .png, .dmg).

Reason of a directory size being zero

Was doing some copy operation on a linux machine to the director /misc. and it did not perform well. After digging came to know that size of /misc is 0.
Logs:
[root#gd911-linux-host1 misc]# ls -lrt
total 0
[root#gd911-linux-host1 misc]#
But created a directory myself which empty only & checked its size, showing 4 bytes.
Logs:
[root#gd911-linux-host1 random]# du -sk
4 .
[root#gd911-linux-host1 random]#
Please let me know what is the reason of this.
Both commands are functioning 100% correctly. When you create a directory, for example:
mkdir -p misc
The directory is empty. If you use ls to list the files in the directory, it reports as expected:
$ ls -lrt misc
total 0
Because there are 0 files in the directory. Now when you look at du the disk usage taken by the directory itself, it correctly notes the size of the file (inode, links, etc.) that represents the directory on disk. Example:
$ du -sk misc
4 misc
Rest assured both are working correctly on your system. ls reporting the number of files contained within the directory, and du reporting the actual size that the directory itself takes on disk.
Stricto sensu, a directory cannot be completely empty (on Linux and POSIX systems). It is required to have the two entries for . (the directory itself) and .. (the parent directory). Use ls -als to list all entries in some directory. See ls(1) & stat(1). If using * be aware of globbing by the shell (the shell's globbing for * is skipping file names starting with ., see glob(7))
In particular opendir(3) & readdir(3) will give at least these two . & .. entries (except on failure).
Hence, a directory is always taking some place for those two mandatory entries and for additional metadata (i.e. inode).
Your /misc might have not been filled by previous cp commands perhaps for lack of permissions. Be sure that its owner is appropriate (the one used by cp commands). Check with stat /misc or with ls -ald /misc. Then chmod u+rwx /misc at least (see chmod(1); you could use as root the chown(1) command to change ownership).

Resources