PDA

View Full Version : Bash Cheet Sheets



fb1h2s
12-10-2012, 03:14 PM
Rename all files in a directorty:
All files would be renamed to .jpeg extension.


Ex: rename 's/(.*)/$1.jpeg/' *

fb1h2s
12-10-2012, 03:17 PM
bash Find Files containing Strings:
credits :dzone.com


grep -lir "some text" *

fb1h2s
12-10-2012, 03:19 PM
Will print all results where some string is not found:


grep -v "some string"

Narcissus
11-26-2013, 11:28 PM
Ohh my !

This thread should grow ... I mean really grow.
Bash magic sometimes can be pretty amazing.
Kudos fb1h2s !

-

Narcissus

c0dist
11-29-2013, 01:08 PM
Faster listing of files



ls -f
or
ls -U


This is generally faster because using either of the switches ensures the files are not sorted and are listed as stored.

Note: This can be used with `wc -l` for counting files in a directory. This works faster than typical `ls -l | wc -l`


ls -f | wc -l



Source: http://www.gnu.org/software/coreutils/manual/html_node/Sorting-the-output.html#Sorting-the-output

Hackuin
12-02-2013, 05:16 PM
ss

Much faster then usual "netstat"

And all "ip" commands


ip addr
ip link
ip route

Source: Why ifconfig sucks (http://inai.de/2008/02/19)

cipher
08-25-2014, 09:57 AM
Basically all commands typed on the terminal can we viewed in the bash_history file. When you give space and type the command in the terminal it will not be logged in the history.


user@localhost:~#[space]shutdown now

cipher
08-25-2014, 07:28 PM
Whatever command we type on the terminal is logged in the ~/.bash_history file and can we viewed with the history command. But when you give a space and then type the command, the command will successfully run without being logged in the ~/.bash_history file. bash_history file is an hidden file in the users home directory

Eg: user@box:~$[space]shutdown now

b0nd
08-26-2014, 12:53 PM
Basically all commands typed on the terminal can we viewed in the bash_history file. When you give space and type the command in the terminal it will not be logged in the history.


user@localhost:~#[space]shutdown now

Had discussion on same long time back :)
http://www.garage4hackers.com/showthread.php?t=1442

cipher
09-03-2014, 02:11 AM
Had discussion on same long time back :)
http://www.garage4hackers.com/showthread.php?t=1442

I read it.. What if same technic is being used by an attacker on a prod server?
In that case we have to write a keylogger & run as a service.. any other suggestion will be appreciate :):):)

c0dist
09-05-2014, 10:11 AM
Hello all,

I'm pretty sure we all have been there when we opened a root owned file with vim and didn't use sudo. The file is opened in readonly mode and you've already made the changes, but you can't save the changes. Here's a simple trick to keep the changes you've made,

When in insert mode in vim, press 'Esc' key to enter the command mode and enter the following command

:w !sudo tee %

Here's the explanation part for the curious ones. :D
http://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work/7078429#7078429

Hope it helps.

c0dist
09-30-2014, 12:52 PM
Hello,

There are times when we just want add a single line in some file. So, instead of opening editor, we simply can use "echo 'your text' >> somefile". However, this fails when you try to "sudo echo" into a privileged file. So, here are two ways to do that,



c0dist@ubuntu ~ $ echo ' echo "#damn" >> /etc/resolv.conf ' | sudo bash

OR

c0dist@ubuntu ~ $ echo '# blah' | sudo tee --append /etc/resolv.conf # This also prints the changed content on screen.

OR

c0dist@ubuntu ~ $ echo '# blah' | sudo tee --append /etc/resolv.conf > /dev/null # This won't print anything on console.


Source of last two tricks - http://stackoverflow.com/questions/84882/sudo-echo-something-etc-privilegedfile-doesnt-work-is-there-an-alterna/550808#550808
Can't remember where I read the first trick but it's saved in my brain. :D

Regards,
c0dist.