Categories
Linux shell

Bash killer one liners

[et_pb_section admin_label=”Section” fullwidth=”on” specialty=”off”][et_pb_fullwidth_post_title admin_label=”Fullwidth Post Title” title=”on” meta=”on” author=”off” date=”on” categories=”on” comments=”off” featured_image=”on” featured_placement=”background” parallax_effect=”off” parallax_method=”on” text_orientation=”center” text_color=”dark” text_background=”on” text_bg_color=”rgba(255,255,255,0.9)” module_bg_color=”rgba(255,255,255,0)” title_all_caps=”off” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[/et_pb_fullwidth_post_title][/et_pb_section][et_pb_section admin_label=”section”][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”bottom above footer” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[/et_pb_text][et_pb_divider admin_label=”Divider” color=”#ffffff” show_divider=”off” divider_style=”solid” divider_position=”top” hide_on_mobile=”on”]

[/et_pb_divider][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[jetpack-related-posts]

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section][et_pb_section admin_label=”section”][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text”]

We sometimes need to find a process and kill it regularly, so why not automate it? well its not exactly straight forward when you automate things. you don’t want to accidentally kill random processes etc. so here’s a nice clean way to find and kill a process a couple of different ways.

First if you have a process id via a pid file (the way lane)

kill $(cat /path/to/PID-file)

now if you don’t have that you can still get it and do this via the process name

kill $(ps aux | grep '[p]rocess name' | awk '{print $2}')

or

kill $(ps aux | awk '/[p]rocess/ {print $2}')

PS the reason for the [] around the first char is so the look up command itself doesn’t show in the list.

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Categories
Linux shell

Linux su with custom shell

I have some server only users for things like CI or media streaming that I have removed shell access from for security. sometimes however I need to login to them to test something or fix something. well here’s a very easy way to su over to them without setting their shell to something else and then setting it back.

Normally you would use

<pre lang=’bash’>su username</pre>

instead just use

<pre lang=’bash’>su -s /bin/bash username

Categories
Linux shell

Padding File names with zeros

need to pad some file names with zeros? couldnt be simpler!

 

for f in foo[0-9]*; do mv $f `printf foo%05d ${f#foo}`; done
rename 's/\d+/sprintf("%05d",$&)/e' foo*
Categories
Linux shell

SSHFS mount remote file system locally over SSH

[et_pb_section admin_label=”Section” fullwidth=”on” specialty=”off”][et_pb_fullwidth_post_title admin_label=”Fullwidth Post Title” title=”on” meta=”on” author=”off” date=”on” categories=”on” comments=”off” featured_image=”on” featured_placement=”background” parallax_effect=”off” parallax_method=”on” text_orientation=”center” text_color=”dark” text_background=”on” text_bg_color=”rgba(255,255,255,0.9)” module_bg_color=”rgba(255,255,255,0)” title_all_caps=”off” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[/et_pb_fullwidth_post_title][/et_pb_section][et_pb_section admin_label=”section”][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

SSHFS is a filesystem client based on the SSH File Transfer Protocol. Since most SSH servers already support this protocol it is very easy to set up: i.e. on the server side there’s nothing to do. On the client side mounting the filesystem is as easy as logging into the server with ssh.

So we have two systems remote.kpsn.pie and local.kpsn.pie. we want to mount directory /home/homer/supercooldir from remote.kpsn.pie as /home/user/remote in local.kpsn.pie and for this discussion on remote system your user is called homer. we will be doing all these commands as user on local system and not root.

This requires SSH key based authentication to be working between the machines prior to this being setup. check out other tutorial on this setup.

so first off you install sshfs on local.kpsn.pie

sudo apt-get install sshfs

Now you need to make the folder in local.kpsn.org.pie where you will be mounting the remote folder so:

mkdir /home/user/remote

now we go about actually mounting the remote directory to the local one we made so:

sshfs -o idmap=user [email protected]:/home/homer/supercooldir /home/user/remote

What if you have a different port for ssh on remote? well simply use the -p flag, say the port is 12345:

sshfs -p 12345 -o idmap=user [email protected]:/home/homer/supercooldir /home/user/remote

to unmount you type:

fusermount -u /home/user/remote

to automagically mount this at startup edit /etc/rc.local

nano /etc/rc.local

and insert this line ABOVE the line saying exit 0

cd /home/user && su -c "/usr/bin/sshfs -o idmap=user [email protected]:/home/homer/supercooldir /home/user/remote" user

that last “user” is the local machine user. this allows the command to be run as the local machine user so the drive gets mounted properly.

this all is written for ubuntu but should work elsewhere. lemme know of any questions or concerns.

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”bottom above footer” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[/et_pb_text][et_pb_divider admin_label=”Divider” color=”#ffffff” show_divider=”off” divider_style=”solid” divider_position=”top” hide_on_mobile=”on”]

[/et_pb_divider][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[jetpack-related-posts]

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Categories
Linux shell

Directory FileCount

I have close to 3000 images in a folder and needed to know exactly how many for a stupid bet I made. I dont feel like counting them! there must be something in the linux world to make this easier…. well there is. simple extension of the ls command.

my directory is called images but I’m sure contains no images whatsoever. *cough* so lets get the basics :

ls -1 images | wc -l

what if you wanted to not count sub directories or symlinks?

find images -maxdepth 1 -type f | wc -l

want to add symlinks?

find images -maxdepth 1 -type f -follow | wc -l

so yeah the basics should be done 🙂

Categories
Linux regex

Sed 101

SED aka stream editor is a utility app in linux / bsd systems (among others) which allows you to parse text in files based on simple regexp and change them based on its own set of rules.

simple syntax for parsing and replacing text in a file called test.txt that contains the text I love cats and hate dogs n number of times and replace with I love sloths and hate ducks

sed -e 's/cats/sloths/' test.txt
sed -e 's/dogs/ducks/' test.txt

but to make it shorter you can actually add the options to same command.

sed -e 's/cats/sloths/g' -e 's/dogs/ducks/g' test.txt

the problem with above? it will only do so once per line, so if oyu have multiple instances it wont work. so you need to “globalize” this search/replace. note the added g after the words

sed -e 's/dogs/ducks/g' test.txt

what if you only want the replacements to happen on lines 2 – 7?

sed -e '2,7s/dogs/ducks/g' test.txt

How about if you want to only replace on lines begining with the word link? (replace link with any other word you please) and ending in the word pie (again replace pie with whatever)

sed -e '/^link/,/^pie/s/dogs/ducks/g' test.txt

what if your words have the separator / already in them? say in a path like /home/kingpin ? well you can replace the separator with anything arbitrary you choose at the beginning after the s and it will change the separator all throughout the sed command.

sed -e 's;dogs;ducks;g' test.txt

or with / in the words

sed -e 's;/home/kingpin;/home/notkingpin;g' test.txt

 

Well this is it for now… theres SO much more out there, tye man sed in your CLI to find out more or just google 🙂