Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

shell

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.

like image 248
Amumu Avatar asked Dec 11 '11 04:12

Amumu


People also ask

Can a file start with a period?

Also, keep these rules in mind. Don't start or end your filename with a space, period, hyphen, or underline. Keep your filenames to a reasonable length and be sure they are under 31 characters.

How do you go to the beginning of a line in the shell?

Move Cursor on The Command LineCtrl+A or Home – moves the cursor to the start of a line. Ctrl+E or End – moves the cursor to the end of the line.

What is period in shell script?

The dot command ( . ), aka full stop or period, is a command used to evaluate commands in the current execution context. In Bash, the source command is synonym to the dot command ( . ) and you can also pass parameters to the command, beware, this deviate from the POSIX specification.

What are files that start with dot?

In Unix-like operating systems, any file or folder that starts with a dot character (for example, /home/user/. config), commonly called a dot file or dotfile, is to be treated as hidden – that is, the ls command does not display them unless the -a or -A flags ( ls -a or ls -A ) are used.


3 Answers

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?

like image 86
Keith Thompson Avatar answered Oct 23 '22 18:10

Keith Thompson


Wrong place for this question, but it's simple enough:

ls -lhd .*

like image 32
Matt Lacey Avatar answered Oct 23 '22 20:10

Matt Lacey


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

like image 2
Tudor Constantin Avatar answered Oct 23 '22 18:10

Tudor Constantin