Free cookie consent management tool by TermsFeed Policy Generator

/dev/blog/ID10T

Linux: Select all files in a folder

Linux Comments

Advertisement

I wrote a very similar post nearly three years ago (in German) and still need this daily. As I’m currently writing a lot of Dockerfiles, I realised my post wasn’t completely accurate. Although I redacted it, it’s a good opportunity to port the original post to the english language.

Requirement:

Select all files in a directory, including hidden ones or files beginning with crude symbols. But exclude . and .. as those will most likely lead to an exit code > 0.

Lets assume we want to chown all files in the directory /var/www/html:

root@9d5788a0c8d7:/var/www/html# ls -la
total 60
-rw-r--r--.  1 root     root         2 Nov 30 09:25 --test
-rw-r--r--.  1 root     root         2 Nov 30 09:25 -test
drwxr-xr-x.  5 www-data www-data  4096 Nov 30 09:25 .
drwxr-xr-x.  3 root     root      4096 Nov 20 08:35 ..
-rw-r--r--.  1 root     root         0 Nov 30 09:24 ..a
-rw-r--r--.  1 root     root         0 Nov 30 09:24 ..aa
-rw-r--r--.  1 root     root         0 Nov 30 09:24 ..aaa
-rw-r--r--.  1 root     root         0 Nov 30 09:24 .a
-rw-r--r--.  1 root     root         0 Nov 30 09:24 .aa
-rw-r--r--.  1 root     root         0 Nov 30 09:24 .aaa
-rw-rw-r--.  1 root     root      1509 Aug 23 13:57 .htaccess.dist
-rw-rw-r--.  1 root     root       306 Aug 23 13:57 README
-rw-rw-r--.  1 root     root        23 Aug 23 13:57 VERSION
drwxrwxr-x.  2 root     root      4096 Nov 30 09:23 conf
drwxrwxr-x. 12 root     root      4096 Aug 23 13:57 data
-rw-rw-r--.  1 root     root     19372 Aug 23 13:57 feed.php
drwxrwxr-x.  3 root     root      4096 Nov 30 09:22 lib

Notice how we got a lot of strange filenames here, including files beginning with dashes or double dots.

Advertisement

Solution:

A combination of three Shell Wildcards solves our problem:

root@9d5788a0c8d7:~# chown -R www-data:www-data /var/www/html/* /var/www/html/.[!.]* /var/www/html/.??*
root@9d5788a0c8d7:~# echo $?
0
root@9d5788a0c8d7:~# ls -la /var/www/html/
total 60
-rw-r--r--.  1 www-data www-data     2 Nov 30 09:25 --test
-rw-r--r--.  1 www-data www-data     2 Nov 30 09:25 -test
drwxr-xr-x.  5 www-data www-data  4096 Nov 30 09:25 .
drwxr-xr-x.  3 root     root      4096 Nov 20 08:35 ..
-rw-r--r--.  1 www-data www-data     0 Nov 30 09:24 ..a
-rw-r--r--.  1 www-data www-data     0 Nov 30 09:24 ..aa
-rw-r--r--.  1 www-data www-data     0 Nov 30 09:24 ..aaa
-rw-r--r--.  1 www-data www-data     0 Nov 30 09:24 .a
-rw-r--r--.  1 www-data www-data     0 Nov 30 09:24 .aa
-rw-r--r--.  1 www-data www-data     0 Nov 30 09:24 .aaa
-rw-rw-r--.  1 www-data www-data  1509 Aug 23 13:57 .htaccess.dist
-rw-rw-r--.  1 www-data www-data   306 Aug 23 13:57 README
-rw-rw-r--.  1 www-data www-data    23 Aug 23 13:57 VERSION
drwxrwxr-x.  2 www-data www-data  4096 Nov 30 09:23 conf
drwxrwxr-x. 12 www-data www-data  4096 Aug 23 13:57 data
-rw-rw-r--.  1 www-data www-data 19372 Aug 23 13:57 feed.php
drwxrwxr-x.  3 www-data www-data  4096 Nov 30 09:22 lib

Lets inspect those three a little closer…

Not the most intuitive way, but it checks out. If you want to have different solutions, check Superuser.

Advertisement

comments powered by isso

Advertisement