megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

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! ๐Ÿ˜Ž

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!

Exclude directories while searching for a pattern in files

Looking for a specific text pattern in a directory but wanted to avoid some paths? Here's a quick command that you can try:

grep -R --exclude-dir=path/to/directory 'some pattern' /path/to/search

Hope this helps you out!

Fetch selected files from your remote repository

Wanted to fetch a specific file from your Git repository except that the repository doesn't exist in your local machine?

Try this out:

git init
git remote add origin <your_repo_link>.git
git fetch
git checkout <your_branch_name> -- </path/to/file>

After executing these commands, you should be able to see the selected directory/file in your project directory.

Hope you found this 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!

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!

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!

How to recover from an errorneous forced git commit?

If you're the type of person who types git push -f origin master, please don't do that as it might overwrite your entire branch. I'm saying this because I did this once and I thought I lost all the files.

Luckily, I was a bit relieved as git is a VCS (Version Control Software), which means the files are most likely not deleted. This is when I came across git reflog command.

According to the Git manual, this is what it does:

Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.

This is a life-saver especially if you wanted to return back to the previous point in time. Here's how I recovered my files back again:

  1. Type git reflog show remotes/origin/master
  2. Find and make note of the previous commit hash.
  3. Create a new branch with using the previous commit hash like this: git branch <new_branch_name> <previous_commit_hash>
  4. Then finally, push the files to the new branch: git add . && git commit -m "pushing recovered files" && git push origin <new_branch_name>
  5. Checkout to the newly created branch: git checkout <new_branch_name>
  6. Delete the corrupted branch and replace it with the newly created branch that contains your restored files.

If I didn't discover this, I don't really know what I would have done to recover those files.

References

Hope this helps you out!