Linux tricks I use in day to day work

in #utopian-io6 years ago (edited)

image.png

Hey,

I would like to share few of my Linux tricks and commands I use in day to day work. I know everyone has own best practices but I hope that maybe some of the following will be useful for you too ;-)

1. rm: cannot remove file: Permission denied

When you executed command but forget about sudo ;-)

$ rm file_to_remove
rm: cannot remove 'file_to_remove': Permission denied
$ sudo !!
sudo rm file_to_remove

 
!! - repeats the last command in Bash

2. Vim: E212: Can't open file for writing

You get permission error when trying to save recent changes... sounds familiar? Do you know you can use sudo directly from Vim?

:w !sudo tee %

3. ctrl+r, search in bash history

When you run commands in console, Bash save the history in a .bash_history file. If you want to re-run particular command but you don't remember the exact syntax, you can search for it,

ctrl+r
(reverse-i-search)`': part_of_the_command_you_are_searching_for

 
click ctrl+r again for the next result ;-)

image.png

alternative way is running: history | grep part_of_the_command_you_are_searching_for

4. Where to find the ascii table?

Do you know the ascii table is already in manual page?

$ man 7 ascii

image.png

5. Config file backup

If you're going to edit any configuration file it's always a good practice to make a backup of the file. Usually we run,

$ cp config.cfg config.cfg.bak

 
but there is a quicker way ;-)

$ cp config.cfg{,.bak}

6. Back to the previous directory

If you changed the directory to another and want to back to the previous one, you can run

$ cd -

 
This works because Bash keep the current and previous locations in $PWD and $OLDPWD environment variables.

7. Generate test file with particular size

If you need test file with particular size, you can use dd tool,

$ dd if=/dev/zero of=file count=1024 bs=1M
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 13.2885 s, 80.8 MB/s


  • if is the source file/device (/dev/zero is block device which prints "0" characters)
  • of is the output file/device name
  • count is for the number of blocks we want to write,
  • bs is the block size,

8. Delete files older than X...

If you want to delete files older than X days from directory, you can use a tool called find.

$ find /var/log/myapplication/ -type f -mtime +100 -delete

 
this simple command will remove all files older than 100 days from /var/log/myapplication/.


Please let me know if this is something you would like to read in future and don't forget to share your tricks and commands in comments ;-)

$ logout


Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Another sweet Bash shortcut: ALT + . it will paste the previous command last argument. Press it again to browse another previous commands arguments (it always takes only last argument but still this is useful). For example:

$ grep -B2 -A20 NullPointerException myapp.log > npe.txt
$ less <press ALT .>
$ less npe.txt 

Thanks, You remind me one more ;-) If you made a typo in command you can replace character(s) and run it again using substitute feature ;-)

$ whozmi
-bash: whozmi: command not found
$ ^z^a
whoami
jamzed

Congratulations @jamzed! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the total payout received
Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

What is this console? Is it in linux?

Good point, it should be Linux, I'm going to update this.

Your contribution cannot be approved as it does not fit any category for which contributions are currently being accepted.

You can contact us on Discord.
[utopian-moderator]

This post has received a 2.34 % upvote from @buildawhale thanks to: @jamzed. Send at least 1 SBD to @buildawhale with a post link in the memo field for a portion of the next vote.

To support our daily curation initiative, please vote on my owner, @themarkymark, as a Steem Witness

Thank you for your contribution.
It was not approved due to it being more of a tutorial.
If you would like to share similar content, please create it under the tutorial category, with linux being the relevant repository.

You can contact us on Discord
For posting rules, check Rules
[utopian-moderator]

Hey, thank you ;-) I couldn’t figure out which repository is the best, but if Linux is fine, next time I’ll use it ;-)