Linux show hide files

How to show only hidden files in Terminal?

I have a directory that contains thousands of files, some of them are hidden. The command ls -a list all files, including hidden ones, but I need just to list hidden files. What command should I use?

23 Answers 23

Will only list hidden files .

 -l use a long listing format -d, --directory list directory entries instead of contents, and do not derefer‐ ence symbolic links .?* will only state hidden files 

You don’t need the two question marks in there, the * covers it ( ? only matches any single character, * matches any number of them ).

@psusi, I think the intent is to exclude . and .. from the match. However it will also exclude (perfectly legal) single-character hidden filenames such as .a , .1 and so on. Perhaps a better extended glob would be .!(|.) i.e. literal dot followed by anything except nothing or another (single) dot i.e. ls -d .!(|.)

@steeldriver, neat, the ?? version does exclude «.» and «..». This seems to be the result of an interesting quirk: neither ? nor * will match a dot, but the ? must match something otherwise the name is ignored.

To identify directories and files add the F option, i.e., ls -ldF .?* directory names have «/» as last displayed character files don’t.

This almost works except it also list hidden folder like .vim, which I consider not a file here. I modify it a bit like ls -ldp .?* | grep -v / This only list hidden files not hidden folder

Does exactly what OP is looking for .

list directory anything with a . and do not list anything without one is what it pretty much translates into.

I understand that in !(pattern-list) , pattern-list is a list of one or more patterns separated by a | , but it is confusing for me that you don’t have any pattern to the left of | . Could you please explain that syntax to me?

@CarlosMendoza that would become the empty pattern, so this is like . not followed by (nothing or . ), excluding . itself and .. .

Don’t forget to set extglob before using this solution! It may not be enabled by default. shopt -s extglob

If you just want the files in your current directory (no recursion), you could do

That will print the names of all files whose name starts with a . and is followed by one or more non-dot characters. Note that this will fail for files whose name starts with consecutive dots, so for example . foo will not be shown.

find -mindepth 1 -prune -name '.*' 

The -mindepth ensures we don’t match . and the -prune means that find won’t descend into subdirectories.

Explanation:

find . -type f —> List all the files in the current directory along with it’s path like,

/ as field separator awk checks for the last field staring with a dot or not. If it starts with a dot, then it prints the last field of that corresponding line.

Читайте также:  Xorg conf alt linux

find is usually a better option for complicated searches than using name globbing.

find . -mindepth 1 -maxdepth 1 -name '.*' 
find . -mindepth 1 -maxdepth 1 -name '.*' -o -name '*~' 

find . searches current directory

-mindepth 1 excludes . and .. from the list

-maxdepth 1 limits the search to the current directory

-name ‘.*’ find file names that start with a dot

-name ‘*~’ find file names that end with a tilde (usually, these are backup files from text editing programs)

However, this and all of the other answers miss files that are in the current directory’s .hidden file. If you are writing a script, then these lines will read the .hidden file and display the file names of those that exist.

if [[ -f .hidden]] # if '.hidden' exists and is a file then while read filename # read file name from line do if [[ -e "$filename" ]] # if the read file name exists then echo "$filename" # print it fi done < .hidden # read from .hidden file fi 

What's the .hidden file? Why would there ever be a file called .hidden that contains the file names? Anyway, if there is one why would you do something that complex when all you would need would be cat .hidden ? Your find command is correct(ish) but the -name '*~' is irrelevant. Files that end in tildes are backup files but not hidden in any way.

@terdon The .hidden file is for files and folders you want to hide when you can't change the file/folder name to start with a dot. As for files that end in tildes, it depends on the system. ls -B will ignore such files, as will most GUI file explorers.

cat .hidden may show files that no longer exist if those files were deleted or moved since being added to the .hidden file.

I think that you can do it with following command.

ls -a | grep "^\." | grep -v "^\.$" | grep -v "^\..$" 

ls -a command you entered, that shows all files and directories in current working directory.

grep "^\." command I appended, that filters output to shows only hidden files(It's name starts with "." ).

grep -v "^\.$" | grep -v "^\..$" command I appended, that filters output to exclude ., ..(They are current and parent directory).

If some filenames can have more than a line with "\n" , above example could be incorrect.

So I suggest following command to solve it issue.

What else you could have done, is ls .?* Or ls .!(|) that will show you everything in the current dir hidden files/dirs on the top and other files/dirs below

$ ls .?* .bash_history .dmrc .macromedia .weather .bash_logout .gksu.lock .profile .wgetrc .bash_profile .bashrc.save .ICEauthority .toprc .Xauthority .bashrc .lastdir .viminfo .xsession-errors .bashrc~ .dircolors .lynxrc .vimrc .xsession-errors.old . Baron .adobe: Flash_Player .aptitude: cache config .cache: compizconfig-1 rhythmbox dconf shotwell 

Now notice in the above results, it shows you every file/dir with its subdir and any hidden files right below.

[1:0:248][ebaron@37signals:pts/4][~/Desktop] $ ls .!(|) .bash_aliases .bashrc1 .bashrc1~ . askapache-bash-profile.txt examples.desktop Public top-1m.csv backups Firefox_wallpaper.png PycharmProjects top-1m.csv.zip Desktop java_error_in_PYCHARM_17581.log Shotwell Import Log.txt topsites.txt Documents Music Templates Videos Downloads Pictures texput.log vmware 

Sorry, I cannot comment. to explain the difference here between ls .?* and @cioby23 answer ls -d .[!.]* . * And why it is actually printing hidden files twice is because literally you're asking twice . * , .?* , .[!.]* they're the same thing, so adding any of them with different command characters will print twice.

Читайте также:  Linux find in man page

Источник

This post and this website contains affiliate links. See my disclosure about affiliate links.

how to show or display hidden files in linux

In Linux, as you should already know, there is the concept of hidden files and hidden folders. It is not exactly hidden in the literal sense, but all that means is that the file managers and file system utilities will not display these types of files (or folders) by default.

  • These files are usually a mechanism to store user preference or system files that are not modified by user regularly.
  • They are also used by different utilities to store configuration and state of the programs. As these files are not actively used by user on a normal day-to-day basis, it makes sense to hide them in most cases.
  • It also allows the file manager utilities to prevent cluttering up the user interface and provide a soft division between user files and user specific configuration files.

Any file or folder whose name start with a dot (.) is a hidden file, also known as dot file. These files will not be displayed by default when listing the contents of a folder. These files can be referenced just as any file, by using the name of the file (including the dot).

We will see how you can view these files using the most popular directory listing commands and file managers.

ls command

The ls command is probably the most used command line utility and it lists the contents of the specified directory. In order to display all files, including the hidden files in the folder, use the -a or –all option with ls.

This will display all the files, including the two implied folders: . (current directory) and .. (parent folder). If you want to omit the display of these two folders, then use the -A or –almost-all option.

This is quite useful, if you are using the output of the command as input to some other script. You probably do not the script to loop in the current folder (depending on the script).

If you want to display only the hidden files, then you will need to specify a regular expression with the ls command., the following will display just the hidden file and folders.

The -d option is to ensure that the directory contents are not printed out for each directory in the list.

dir command

Another popular command used to display directory contents is dir. Almost all options for dir is the same as ls, which means everything that was shown for ls in the previous section will work for dir as well.

Читайте также:  Linux массовое изменение прав

will display all files, hidden files and the implied folders (. and ..).

will display all files, folders including the hidden folders but excluding both . and ..

will display just the hidden files and hidden folders.

KDE File Manager (dolphin)

The default file manager in KDE is Dolphin. The default setting in Dolphin is not to display hidden or dot files. There are couple of different ways you can enable the option here.

The easiest is probably the keyboard shortcut Alt+. (Alt and dot). You can easily enable the display and disable it again using the same shortcut.

The other option is using the menu option. Click on the Hamburger icon on the menu bar (for Settings/Configuration). In the drop down menu, you will see the option named Show Hidden Files. Click and select it on it to enable the display of hidden files.

hidden files settings in dolphin

You can leave that option selected, if you want to always display the hidden files. The other commonly used file manager is Konqueror, which uses embedded dolphin to display the file system, as well.

Gnome File Manager (files or nautilus)

The default file manager in Gnome on most distros is Gnome Files. It was formerly known as Nautilus. The keyboard shortcut to display hidden files in Nautilus is Ctrl+H. This shortcut can be used to toggle the display of dot files.

The other option is to change it in the configuration. Open Edit -> Preferences and navigate to the Views tab. Select the option Shown hidden and backup files. In modern or latest versions, this option is in Files -> Preferences menu.

hidden files in Gnome files

Xfce File Manager (thunar)

Xfce is a popular light weight desktop environment, and the default file manager is thunar. The keyboard shortcut is display hidden files is again Ctrl+H just as with Gnome File Manager.

You can find the option with in the menu as well, as with other file managers. Click on View in the menu bar, and select Show Hidden Files option.

hidden files in thunar

Midnight Commander

Midnight Commander is a command line based file manager which has a loyal following. The keyboard shortcut to display dot files here is Alt + . (Alt-Period).

There is also a configuration setting with in Panel Options. Open Options from the menu and then Panel Options. Select the option Show Hidden Files.

midnight commander files options

No matter which file manager you are using, there should be an option to display hidden files. Most times, it is disabled by default and as it should be. You can try first by right clicking and checking the context menu. The next place to check is either the Settings or Preferences dialog which is often in the Edit or View menu.

As the last resort, read the manual.

Источник

Оцените статью
Adblock
detector