Posted: . At: 1:28 PM. This was 1 year ago. Post ID: 17019
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Some very interesting Linux shell tricks.


To list a directory without ls, use this simple one-liner.

┌──(john㉿DESKTOP-PF01IEE)-[~/Documents/malware/Symbiote]
└─$ for file in *; do stat -c '%a %U:%G %n %F' $file; done;
644 john:john 121157e0fcb728eb8a23b55457e89d45d76aa3b7d01d3d49105890a00662c924.elf.x86_64 regular file
644 john:john 45eacba032367db7f3b031e5d9df10b30d01664f24da6847322f6af1fd8e7f01.elf.x86_64 regular file
644 john:john a0cd554c35dee3fed3d1607dc18debd1296faaee29b5bd77ff83ab6956a6f9d6.elf.x86_64 regular file
644 john:john ec67bbdf55d3679fca72d3c814186ff4646dd779a862999c82c6faa8e6615180.elf.x86_64 regular file
644 john:john f55af21f69a183fb8550ac60f392b05df14aa01d7ffe9f28bc48a118dc110b4c.elf.x86_64 regular file
644 john:john out.s regular file

The output is a bit messy, but this does work. We are using stat to get the file information.

Another way is to use the lsattr command, this can be used to list files in a folder and the file attributes.

┌──(john㉿DESKTOP-PF01IEE)-[~/Documents/malware/Symbiote]
└─$ lsattr 
--------------e------- ./a0cd554c35dee3fed3d1607dc18debd1296faaee29b5bd77ff83ab6956a6f9d6.elf.x86_64
--------------e------- ./out.s
--------------e------- ./121157e0fcb728eb8a23b55457e89d45d76aa3b7d01d3d49105890a00662c924.elf.x86_64
--------------e------- ./45eacba032367db7f3b031e5d9df10b30d01664f24da6847322f6af1fd8e7f01.elf.x86_64
--------------e------- ./ec67bbdf55d3679fca72d3c814186ff4646dd779a862999c82c6faa8e6615180.elf.x86_64
--------------e------- ./f55af21f69a183fb8550ac60f392b05df14aa01d7ffe9f28bc48a118dc110b4c.elf.x86_64

This gives a clean output when listing files in a directory.

Yet another way to list the contents of a directory is using find with the -exec flag to run the stat one-liner.

┌──(john㉿DESKTOP-PF01IEE)-[~/Documents/malware/Symbiote]
└─$ find ./ -maxdepth 1 -exec stat -c '%a %U:%G %n %F' {} \;
755 john:john ./ directory
644 john:john ./a0cd554c35dee3fed3d1607dc18debd1296faaee29b5bd77ff83ab6956a6f9d6.elf.x86_64 regular file
644 john:john ./out.s regular file
644 john:john ./121157e0fcb728eb8a23b55457e89d45d76aa3b7d01d3d49105890a00662c924.elf.x86_64 regular file
644 john:john ./45eacba032367db7f3b031e5d9df10b30d01664f24da6847322f6af1fd8e7f01.elf.x86_64 regular file
644 john:john ./ec67bbdf55d3679fca72d3c814186ff4646dd779a862999c82c6faa8e6615180.elf.x86_64 regular file
644 john:john ./f55af21f69a183fb8550ac60f392b05df14aa01d7ffe9f28bc48a118dc110b4c.elf.x86_64 regular file

This is very fast. The -maxdepth 1 flag defines only one level, so we are not searching other subdirectories for files. Create a file with an empty filename.

(base) jason@jason-Lenovo-H50-55:~$ touch '​'

Control-Shift-u and type 200b and then press ENTER, this will create a zero-width invisible character. Then press ENTER, this should create a file named “ ”, this is useless as you cannot read the file, as it would be hard to open. This is what the file looks like in a directory listing.

(base) jason@jason-Lenovo-H50-55:~$ ls -hulai
total 22M
55838323 -rw-rw-r--  1 jason jason    0 Nov 18 13:1155838354 -rw-rw-r--  1 jason jason    0 Aug 25 13:00  ​.​
55838274 drwxr-xr-x 33 jason jason 4.0K Nov 19 07:42  .
55836673 drwxr-xr-x  5 root  root  4.0K Nov 16 09:14  ..

To delete this file, use the inode number shown in the listing above.

(base) jason@jason-Lenovo-H50-55:~$ find . -inum 55838323 -delete

This will allow the file to be deleted no matter what the filename.


1 thought on “Some very interesting Linux shell tricks.”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.