Linux tricks I use in day to day work

Words
379
Reading
2 min
Listen
Play
9y

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

Linux tricks I use in day to day work | Ecency