Linux/Unix Command Line tutorials

Edit a file without changing its timestamp on Linux

Usually anytime a file a edited, its timestamp is also changed. There are cases when you want to change the content of a file but read more

Find which process is listening on a port on Linux

Sometimes we need to find out what process is using or listening on a specific port on Linux machine. This can be fairly useful in read more

Get file size in bytes on Linux

Sometime we need to get file size for the purpose of automation in bash shell script. Here are few ways to get a size of read more

How to delete files starting with dash/hyphen

Deleting files starting with dash or hyphen can be little tricky as dash is interpreted as option prefix on Mac and Linux. Here is what read more

How to kill unresponsive ssh session using escape sequence

Sometime ssh shell become unresponsive when left for too long. It is little annoying to close the terminal to kill the ssh session. To kill read more

How to show environment variable for a process id (pid)

Sometimes it may be useful to see what environment variables were passed to a process. e.g. you may want to see LC_ALL value for a read more

How to sort using a specific field on Linux

Sometime we need to sort a file on a specific field. This may be useful if a file is in csv or tsv formart (probably read more

How to specify environment variable for a command on Linux

Sometime we need to run a command with specific environment variables. These can be either just needed for the command or for the whole bash read more

How to zip/unzip a directory with password

Zip and unzip utility can be used to compress a file or directory on Mac. These should also work on Ubuntu or other Linux flavours. read more

Impact on LC_ALL on Linux sort

Linux sort is a handy Linux command line tool to sort text files. It can sort fairly large files without consuming too much memory. The read more

Linux – find listening ports

Sometimes it is useful to find what ports a machine is listening to. Here are some use cases: Display listening ports $ netstat -l Display read more

Linux – find top directories by used disk size (excluding size of subdirectories)

Find top directories by size is useful to find and delete large files on Linux. One can use du command for this. Change to Linux read more

Linux – how to run a command as different user

Often we need to run a command as different users. Some of these users may not even have a login shell (e.g. www-data on Ubuntu read more

Linux – list only directories

Sometimes we need to list only directories on Linux (or any other unix). Here are some approaches. List directories using ls and shell wildcard $ read more

Linux – providing sudo access to a users – some best practices

Giving sudo to a user is frequently occurring activity on Linux. Here are some scenarios and best practices for providing sudo to users on Linux. read more

Linux – sending mail from command using mailutils

Linux command line tools to send mails are very handy for sending mails from bash scripts. As an example you can send daily php error read more

Linux file timestamps

Linux stores creation, modification and access file timestamp of a file. Here is brief definitions of these three timestamps: Creation, modification and access file timestamp

Linux how to modify a user using usermod

Modifying a user on Linux can be easily done on command line using usermod. These are some scenarios and handy commands to modify a user read more

Linux ping a port using netcat

To check if a port is reachable on Linux This is tested using nc version 7.50 (package nmap-ncat) on Amazon Linux. nc -vz localhost 22Connection read more

Linux replace comma with newline

To replace comma of any other character with newline on Linux or Mac, the following tr command can be used tr ‘,’ ‘\n’ filename or read more

Linux screen – quick start guide

When working on remote Linux terminals its a good idea to use screen manager as it will protect your session from disconnection. Here are some read more

Linux what package provides a file

Using rpm We can use rpm on Centos/RHEL/Amazon Linux based system to file which package provides a specific file. e.g. rpm -q -f /usr/bin/watchprocps-3.2.8-45.16.amzn1.x86_64 Alternatively read more

Linux/Unix – How to go to previous directory

To go to previous working directory is Linux is a frequently occurring need. These are two ways to achieve it: Using dash (-) $ cd read more

Linux/Unix – find inode number of a file

To find the inode number of a file on Linux/Unix the following approaches can be used. Find inode using ls $ ls -i file1.txt 412882 read more

Linux/Unix – truncate a large log file without deleting it

Sometimes we need to truncate (make it size 0) a large opened file by a running process (e.h. php error log, apache error logs, custom read more

Linux/Unix history with date and time

It is useful to run history command with date and time along with each command. Here are instructions on how to do it on Linux/Unix. read more

Memcache – how to dump all keys and values on command line

Memcache is a very simple, distributed memory cache used widely with mysql and other databases. Often we want to inspect what is in memcache server read more

Mongo – cli quick start guide

Using mongo cli utility can be handy way to query mongo db and automate few simple use cases. This article covers some handy commands which read more

Perl command line – replace multi line comments

Perl command line one liner can be used to replace text in a file using regular expressions (regex). It is very powerful approach for automating read more

Python/Perl/Unix one liners

Here are some cool python one liners. Parse apache log using python: print 5th field python -c “import csv,sys;f=csv.reader(sys.stdin, delimiter=’ ‘); print ‘\n’.join([r[5] for r read more

Ruby gem – handy reference

Ruby gem is a package manager for the Ruby programming language. Here are some gem handy commands on Mac or Linux. Install a package $ read more

Some handy linux gnu date commands

Using date command is pretty handy way to covert unix timestamp to string format and vice versa. This can also be used to print unix read more

Use watch to monitor a command at some frequency on Linux

Use watch with -n to watch a command ever 2 seconds. e.g. To watch top command with first 12 lines every 2 seconds:watch -n 2  read more

bower – installation and quick start guide

Bower is a package manager for web front end frameworks, libraries, assets and utilities. Bower can manage components that contain HTML, CSS, JavaScript, fonts and read more

csvkit – parse csv file and data on Linux command line

csvkit can be used to extract and process fields from a csv file on Linux or Mac. Install python3 if not installed already. The command read more

How to do grep without regex (fixed string)

Linux (or Unix) grep without any options does a regex grep on input stream. To disable regex either one can use grep -F or fgrep. read more

redis cli quick start tutorial

Redis is a fast key value cache and store. It is useful to get familiar with some handy redis command line interface (cli) commands. Here read more

wget handy commands

Here are some handy wget commands wget with username and password In case a url requires simple web auth, one can use the following wget read more

Find recently modified files on Linux

There are times when we want to find recently modified files in a directory. We can use command line tool find on Linux or Mac read more

Linux – find and delete files older than 30 days

Often we need to delete old file on Linux or Unix which are old (say 30 days or more). Here are some commands which can read more

Linux – find files containing specific text

Handy command on Linux/Mac to find all files matching a pattern containing a specific text. This will also print file name. $ find . -type read more

Linux find – ignore case in name

When using linux find, -iname can be used to match name pattern and ignore case (case insensitive). $ fine . -iname “pattern” Example Find all read more

find – exclude directory or file pattern

To exclude directory or file pattern when using find on command line one can use ! -name “pattern_to_exclude” Example To exclude a directory using its read more