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!
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!
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!
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!
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!
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!
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:
git reflog show remotes/origin/master
git branch <new_branch_name> <previous_commit_hash>
git add . && git commit -m "pushing recovered files" && git push origin <new_branch_name>
git checkout <new_branch_name>
If I didn't discover this, I don't really know what I would have done to recover those files.
Hope this helps you out!
This error shows up when two different projects are merged (i.e. they both could be the same project but unaware of each other's existence and have different commit histories).
If you are facing this, chances are that:
You must have cloned a project and the .git
directory must have corrupted or got deleted and at this point, Git is unaware of the changes being made and will throw this error when you try to push to or pull from the remote repository.
You created a new repository, made some changes and added the commits and then you tried to pull from the remote repository.
Well, you can easily resolve by passing the --allow-unrelated-histories
flag when pulling the latest updates from the remote repository:
git pull origin master --allow-unrelated-histories
Hope you found this useful!
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 -@
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! ๐