megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Zip all files that are modified on a specific date

I wrote a nifty command to make an archive of the files that I have modified on a particular date. By doing so, this script comes in handy during urgent deployments, so that I don't lose track of the files that I should be updating.

Here's the command at your disposal:

find . -maxdepth 10 -type f -newermt "2021-04-10" | zip -qur archive.zip -@

How this works?

Let's see what this command does in pieces:

find is the utility tool used to return the filenames that match the specified parameters in the given directory.

-maxdepth is the flag that allows you to specify the depth of recursive searches it should perform.

-type is the flag that determines if you're looking for file or a directory.

-newermt is the flag that determines if the file has been modified on and/or after the given date.

Once, the file has been found, the output is being redirected to the zip utility function, which would then add the files to the .zip file. The -q flag would perform the operation in silent mode, -u flag would update the files in the archive if modified or add it as a new file if it doesn't exist and -@ takes the list of files from the standard input.

If you want to ignore certain directories or file extensions, in that case, you can exclude them like so:

find . -maxdepth 30 -type f ! -path "./path/to/directory/*" !  -path "*.ext" -newermt "2021-04-10" | zip -qur archive.zip -@

Or, you can even archive the modified files by specifying a date range:

touch --date "2021-04-10" startdate
touch --date "2021-04-15" enddate
find . -maxdepth 30 -type f -newer startdate -not -newer enddate | zip -ur archive.zip -@

Hope you found this tip useful! 😁

Find directories older than a specific date and sorted by size

Wanted to see which directories were created on an older date along with their sizes? Try this:

find . -type d -maxdepth 1 ! -newermt "2021-04-10" -exec du -sh {} \; | sort -h >> oldprojectsizes.txt

Hope you found this tip useful!

Get a list of directories with their sizes in the current directory

If you want to sizes of each directory in a list, try the following command:

du -sh * | sort -h >> directories-sorted.txt

And, if you wanted to find the directory that takes consumes a lot of space in your directory, you can try this:

du -sh * | sort -h | tail -n 1

Hope these tips do come in handy! 😊

Perform Git operations using path directory

The -C flag means the path of the directory and using this flag, you can perform any Git operations outside the project's directory without having to enter the directory all the time:

git -C /path/to/directory <command>

Hope this tip helps you out!

How to update your Git remote origin URL?

Did you make a typo while typing out your Git remote origin URL? Is it pointing towards the wrong repository?

You can easily update it using the following command:

git remote set-url origin https://example.com/username/repository.git

Hope you found this useful!

How to change the character encoding of a file via Terminal?

Sometimes, I face character encoding issues while making minor edits via a SFTP console connected to a Linux server. I found a quick hack to change the file encoding using vim on the command line.

In this example, I'm changing the encoding of the file to unix:

vim $filename +"set ff=unix" +wq

Hope you found this helpful!

Find directories created within a date range

Executing these commands helps me create a sorted list of files/directories created within a specific date range:

touch -t 202104100000 start
touch -t 202104150000 stop
find . -type d -maxdepth 1 -newer start \! -newer stop | sort >> directories.txt

Someday, these commands shall come in handy, bud! 😎

How to perform mysqldump without a password prompt?

If you're performing a mass database backup using mysqldump, you'll pretty much find it annoying to type in the password every single time.

To get rid of it, open /etc/mysql/my.cnf configuration file and add your database credentials:

[mysqldump]
user="your_username"
password="your_password"

Once done, save the my.cnf configuration file and now, you can try exporting your database without a hassle like this:

mysqldump -u your_username database_name > database.sql

Hope this trick helps you out!