FreeBSD Handy commands
About
So many times I'm looking for something specific and it takes a couple of googles and following mailing lists, and reading blogs and and and... Sometimes it just get too much. So the aim it to keep it as short and descriptive where possible.
Helpful hints
Here is a ongoing list of useful commands one can get use to. Feel
free to comment or sugest any command you feel are usefull to your
daily usage. Or even commands that are negleted in your opinion. These
are linked to other hubs I have created explaining the functions in
more detail.
- search a windows network with samba and grep
- search a windows share with smbclient and grep
- creating your own bootup scripts
- basics: ports vs packages
One Liners
Simple commands to make your life easier. Many of these commands may seem obvious to many who has worked in the *nix enviroment but for the newbies it just might make your life one google search less =]
Clear a file
Instead of deleting a file, then touch it them permissioning it
rm /to/file.ext ; \ touch /to/file.ext ; \ chown user:group /to/file.ext
Just use
echo "" > /to/file
You are just rewriting the file contents with blank ("")
Add to /etc/rc.conf
Adding a new entry to the /etc/rc.conf can be tedious and error prone, redirect the output and append to the file. WARNING make sure you use double arrows OR YOU WILL WIPE YOUT rc.conf. Then again, you should have a backup somewhere... right ?
/usr/local/etc/rc.d/script_name >> /etc/rc.conf
Which version am I running ?
To check which version of FreeBSD you are running
uname -a
Change your default route
You might need to change your default route on how FreeBSD get to an outside network (e.g. LAN or Internet connection
route del defaultroute add default x.x.x.x
View your routes
To see where and how network traffic is routed we use netstat
netstat -rn
What ports are open
To view open ports on your system, or the see what ports are used by an application
sockstat -4l
Find a word in a file
Finding a needle in a haystack. This require that you use a shell that can use for loops and if statements in the command line. First we change shell, do the command and then go back to the default shell
shfor FILE in `find /path/to/dir/`;\ do \ grep --color=auto -iHE 'search' $FILE;\ done
Or a one liner:
echo 'for FILE in `find /path/to/dir/`; do grep --color=auto -iHE "search" $FILE; done' | sh
ls output as csv file
Sending your full structure as a csv file
ls -lR | tr -s " " | tr " " "," > /path/to/file.csv
Quick replace with sed
A nice handy example for using sed is while working with zone files in bind. Not going to get info specifics, the short and sweet is, replacing just a name in a file and createing a copy of the file
cat /path/to/template.domain | sed -E 's/template/newname/g' > newname.domain
Comments
No comments yet.