Mind Chasers Inc.
Mind Chasers Inc.

Linux Command Line Tips

Various Notes on Using and Managing a Linux System from the Command Line

Table of Contents

Notes:

  • A line that start with a "$" denotes a command line entry in a user shell / terminal.
  • '#' can denote two things: root shell or comment. If a line starts with '$', then '#' and what follows is a comment.
  • Most examples will work equally well on an Ubuntu desktop or a Linux system built with Yocto (assuming the right packages are installed).
  • Most commands have many options. Refer to the man pages for more information.

Annoying Bell

Turn off the bell you hear while working on the command line

edit /etc/inputrc:


# do not bell on tab-completion
set bell-style none
	

Cscope

Quickly build a searchable cscope database / index of your C project:


$ find -L . -name '*.[chS]' > cscope.files
$ cscope -b -q -k
$ cscope -d
	

Date

date uses the following format: [MMDDhhmm[[CC]YY][.ss]]


# # assume it's noon on April 22 2019:
# date 042212002019
Mon Apr 22 12:00:00 EDT 2019

# # check how timezone is set:
# ls -l /etc/localtime 
lrwxrwxrwx 1 root root 27 Apr 22 10:12 /etc/localtime -> /usr/share/zoneinfo/EST5EDT

# # change it to central:
# ln -f -s /usr/share/zoneinfo/CST6CDT /etc/localtime
# date 
Mon Apr 22 11:01:45 CDT 2019
	

Additional notes on date and timezone:

  • Use ntp daemon to automatically update date and time
  • The TZ setting can also be modified in /etc/profile

Basic Disk Operations

Tasks for creating a new drive partition mounting, testing, and fixing it:


# # format partitions on an unmounted drive:
# fdisk /dev/sda

# # create a filesystem on the third SDA partition:
# mke2fs /dev/sda3

# # mount a partition at /mnt/drive:
# mkdir -p /mnt/drive
# mount /dev/sda3 /mnt/drive

# # test and fix disk errors on the partition (unmount first)
# e2fsck /dev/sda3 

# # or to automatically fix errors and be verbose:
# e2fsck -vy /dev/sda3
	

Environment

Control environment variables from the command line:


$ export var1="privacy"	 # note there are no spaces

$ echo $var1		# return the value of "var1"
privacy

$ echo "I like my $var1"
I like my privacy

$ unset var1		# remove the environemnt variable

$ echo $var1

$ set | grep var1
$

Find

Non-trivial examples using a very powerful but non-intuitive tool:


$ # find *.py files and copy to a temp directory
$ find . -name '*.py' | xargs -I{} cp {} ~/temp

$ # delete *.js files that aren't minified: 
$ find . -path '*.js' ! -path '*.min.js' | xargs rm

$ # find my js files but exclude those in node_modules and build
find . -path '*.js' ! -path "./node_modules/*" ! -path "./build/*"

$ # Find source files in a C project and include symbolic links
$ find -L  . -name '*.[chS]'

$ # same as above but include cpp files
$ find -L  . -name '*.[chS]' -or -name '*.cpp'

$ # Search my book directory tree for a pdf title, but exclude files that start with a '.'
$ find . -name '[^.]*.pdf' | grep -i <part of title>

Grep

Some useful grep options for searching through files:

	
$ # search recursively for "elf" as a word and case insensitive
$ grep -iwR elf .

$ # do the same thing but exclude binary files 
$ grep -iwIR elf .

$ # find "elf" when it's at the end of a line, note use of regex here
$ grep -iwR 'elf$' .

$ # use expanded / full regexp to search for .html or .htm in file
$ grep -E "\.html?" <file>
	

useful grep command line options:

  • -i: case insensitive
  • -n: output line number
  • -w: select only those lines containing matches that form whole words
  • -I: exclude binary files
  • -R: search into the directories below

Logging

Note: syslog can usually be found at /var/log

	
# # write to the kernel log:
# echo Hello World > /dev/kmsg

# tail -n 1 /var/log/syslog
Apr 22 12:24:39 kernel: <12>Hello World

# # write to syslog and stderr with specific tag
# logger -s -t 'tag' 'hello world again'
<13>Apr 22 12:28:28 tag: hello world again

# tail -n 1 /var/log/syslog 
Apr 22 12:28:28 tag: hello world again
	

man pages

Use the man pages effectively:


$ # search for socket man pages
$ man -f socket		
socket (7)           - Linux socket interface
socket (2)           - create an endpoint for communication
Socket (3perl)       - networking constants and support functions

$ # list socket(7)
$ man -S 7 socket

$ # search for man pages with socket in the short description:
$ man -k socket
Socket (3perl)       - networking constants and support functions
accept (2)           - accept a connection on a socket
accept4 (2)          - accept a connection on a socket
...

$ # walk through each man page for "socket":
$ man -a socket
...

Query a DNS Server with nslookup

Use nslookup to query a Domain Name Server for the IP address of a particular domain.

$ nslookup google.com
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	google.com
Address: 172.217.10.142
Name:	google.com
Address: 2607:f8b0:4006:811::200e

In this case, we see both an IPv4 and IPv6 address. Also, the "127.0.0.53" address is that of our defaut DNS server that we queried. This can be changed by specifying the DNS as a second argument, as shown next:

$ nslookup google.com ns1.google.com
Server:		ns1.google.com
Address:	216.239.32.10#53

Name:	google.com
Address: 172.217.12.206
Name:	google.com
Address: 2607:f8b0:4006:81b::200e

Note that host is another common command line utility for performing DNS queries.

Processor Affinity

Run a command on a particular CPU core


$ # Use a bit mask to specify the CPU 
$ # example: cat the cpu stats using CPU 1 (the second core) 
$ taskset 2 cat /proc/cpuinfo	
	

Python from command line

Execute Python commands without entering interpreter shell:

$ python3 -c "print(hex(0x400<<8))"
0x40000

$ python3 -c "import math; F=10**6; print(2*math.pi*F)"
6283185.307179586

$ python3 -c "import site; print(site.getsitepackages())"
['/usr/local/lib/python3.6/dist-packages', ...]

User and Group ID

This example is run from a freshly installed embedded system as root.

	
# # return our groups:
# groups 
root

# # get our real and effective user & group IDs
# id
uid=0(root) gid=0(root) groups=0(root)
	

Didn't find an answer to your question? Post your issue below or in our new FORUM, and we'll try our best to help you find a solution.

And please note that we update our site daily with new content related to our open source approach to network security and system design. If you would like to be notified about these changes, then please follow us on Twitter and join our mailing list.

Related articles on this site:

share
subscribe to mailing list:

Please help us improve this article by adding your comment or question:

your email address will be kept private
authenticate with a 3rd party for enhanced features, such as image upload
previous month
next month
Su
Mo
Tu
Wd
Th
Fr
Sa
loading