Posted: 28 June 2024. At: 12:20 PM. This was 5 days ago. Post ID: 12226
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.

Very useful ways to create wildcards with the Linux shell.

  1. Count the number of unique IP addresses in an Apache log.
  2. Switch back and forth between folders on Linux.
  3. Use the find command on Linux to find certain files.

This is an example of a wildcard, using the [0-9] and [A-Z] wildcards to look for numbers and capital letters.

deusexmachina:Documents jason$ ls -hula [A-Z]rma[0-9]*
-rwxr-xr-x  1 jason  staff   1.7G  5 Mar 20:14 Arma3_x64_2017_11_13_21_50_20_373.avi
-rwxrwxrwx  1 jason  staff   1.2G 28 Jun 14:04 Arma3_x64_2018_01_04_15_29_53_844.avi
-rwxrwxrwx  1 jason  staff   1.0G  4 Jan 16:13 Arma3_x64_2018_01_04_15_32_03_159.avi
-rwxrwxrwx  1 jason  staff   2.9G 27 Mar 14:30 Arma3_x64_2018_01_04_15_34_52_367.avi

This wildcard example will find all filenames that start with a number.

deusexmachina:Documents jason$ ls -ulha [0-9]*
-rw-r--r--@ 1 jason  staff   163K 20 Feb 12:57 1514877771595.jpg
-rw-r--r--@ 1 jason  staff    51K 18 Jun 11:10 93bffab7a44bc518799a2a68711ad790.jpg

Listing all files that start with letters and end with a number in the filename.

deusexmachina:Desktop Pictures jason$ ls | grep "[a-z] [0-9]"
Color Burst 1.jpg
Color Burst 2.jpg
Color Burst 3.jpg
El Capitan 2.jpg
Sierra 2.jpg
Yosemite 2.jpg
Yosemite 3.jpg
Yosemite 4.jpg
Yosemite 5.jpg

Count the number of unique IP addresses in an Apache log.

This one-liner allows counting the number of unique IP addresses in an Apache log file.

(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 SSL  $ awk '{ print $1 }' sslaccesslog_securitronlinux.com_6_20_2024 | sort -n | uniq -c | sort -nr | head -20

Switch back and forth between folders on Linux.

This is a neat Linux trick. Switch back and forth between folders on Linux.

Firstly, navigate to a folder.

(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 ~  $ cd Documents/

Then use this cd trick to switch straight back.

(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 ~  $ cd -
/home/jcartwright/Documents

Use this trick again to switch back again.

(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 Documents  $ cd -
/home/jcartwright

The pushd built-in command will show the directory stack as I was using before.

(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 ~  $ pushd -
/home/jcartwright/Documents
~/Documents ~

Use the find command on Linux to find certain files.

Using the find command on Linux is very useful for finding files according to the criteria you specify.

This example will find all files above 199 Megabytes in size.

(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 Documents  $ find . -size +199M
./fuck/DHTP-2019_11_17.pk3
./Samsung/0sx1hz.mp4
./win7.img
./Stalker/S.T.A.L.K.E.R.Living_Zone_EXCURSION_Mod 2.34/gamedata/levels/pripyat_full/level.ai
./Stalker/S.T.A.L.K.E.R.Living_Zone_EXCURSION_Mod 2.34/gamedata/levels/pripyat_full/level.cform
./Stalker/S.T.A.L.K.E.R.Living_Zone_EXCURSION_Mod 2.34/gamedata/levels/pripyat_full/level.geom
./Stalker/S.T.A.L.K.E.R.Living_Zone_EXCURSION_Mod 2.34/gamedata/levels/pripyat_full/level.geomx
./backup-1.18.2012_06-19-59_bejiitas.tar.gz
./backup-1.18.2012_06-19-59_bejiitas/homedir.tar
./backup-1.18.2012_06-19-59_bejiitas/homedir (1)/public_html/error_log
./backup-1.18.2012_06-19-59_bejiitas/homedir (1)/public_html/wp-admin/error_log
./quake/id1/QRP_map_textures_v.1.00.pk3
./quake/classic/id1/QRP_map_textures_v.1.00.pk3
./Doom/DOS Doom/DoomMacSource.iso
./Doom/PB_Staging_e501bf7.pk3
./DLD6_01_Lite_-_9-99.iso

Find all folders in a directory and filter for a certain folder name.

(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 Documents  $ find . -type d -name Dragon*
./DragonOnLinux

Leave a Reply

Your email address will not be published. Required fields are marked *

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