Linux Command Line Guide

Published 08-22-2017 20:20:23

Compression

Compress or decompress a bzip file

bzip filename

Create filename.tar with [files]

tar cf filename.tar [files]

Extract files from filename.tar

tar xf filename.tar

Create a new tar file with gzip compression

tar czf filename.tar.gz [files]

Extract a tar using gzip

tar xzf filename.tar.gz

Create a new tar file with bzip2 compression

tar cjf filename.tar.bz2 [files]

Extract a tar using bzip2 compression

tar xjf filename.tar.bz2

Create compressed file filename.gz

gzip filename.gz

Decompress filename.gz

gzip -d filename.gz

File Commands

View a txt file one screen at a time

less text_file

Formatted directory listing with hidden files

ls -al

Show current directory name

pwd

Create a directory

mkdir

Delete file

rm [filename]

Delete directory

rm -r [dir]

Force delete file

rm -f [filename]

Force delete directory

rm -rf [directory]

Copy fileA to fileB

cp fileA fileB

Copy dirA to dirB

cp -r dirA dirB

Rename or move fileA to fileB

mv fileA fileB

Create symbolic link to file

ln -s [filename] [link]

Create or update a file

touch [filename]

Output to file

cat > [filename]
echo > [filename]
printf > [filename]

Output to end of file

cat >> [filename]
echo >> [filename]
printf >> [filename]

Output contents of a file

more [filename]
cat [filename]

Output the first 10 lines of a file

head [filename]

Output the last 10 lines of a file

tail [filename]

Output the contents of a file as it grows

tail -f [filename]

Encrypt file

gpg -c [filename]

Decrypt file

gpg [filename]

Secure Shell

Run command [c] as a user [u] on host [h]

ssh [u]@[h] [c]

Run GUI command on host [h] as user [u]

ssh -f -Y [u]@[h] xeyes

Forward connections to host [h]:8080

ssh -g -l 8080:localhost:80 root@[h]

Forward connections from host [h]:1434 to imap

ssh -R 1434:imap:143 root@[h]

Install user [u] public key in host [h] avoiding password requirement at login

ssh -copy-id [u]@[h]

Copy (with permissions) to user [u]’s home directory on the host @[h]

scp -p -r [u]@[h]: [file] [dir]/

File Transfer

FTP client

ftp
-p Use passive mode
-i Turn the prompts off during multi-transfers
-n Prevent FTP from auto login attempt
-e Disable command editing + history support
-g Disable file name globbing
-v Verbose mode
-d Enable debugging
! Bring up interactive shell
$ Execute a macro
account <password> Supply a password required by remote
ascii Set file transfer to ASCII protocols
bell Ring bell after each command
binary Enable image transfer support
bye Terminate the FTP session + exit
close Terminate the FTP session + return to CMD
open <host> Open a connection to remote <host>
status Show status of FTP server
verbose Show detailed information

Package Management

Install from source

./configure make

Install from .deb package

dpkg -i filename.deb

Install from .rpm package

rpm -Uvh filename.rpm

Install a pacman package

pacman -U filename.tar.pkg.gz

Remove a pacman package and unneeded dependencies

pacman -Rs [package]

Clear pacman cache, force update

pacman -Scc
pacman -Syyu

Refresh pacman keys

pacman-key --refresh-keys

Remove all packages but base group

pacman -R $(comm -23 <(pacman -Qq | sort) <((for i in $(pacman -Qqg base); do pactree -ul "$i"; done) | sort -u))

Network

Check network card and IP information

arp

Configure network interface

ifconfig

Get whois information for the domain

whois [domain]

Get DNS information for the domain

dig

Nslookup on domain

nslookup -type=any [domain]

Reverse lookup host

dig x host

Download file

wget filename

Continue interrupted download of file

wget -c filename

Review network stats

netstat

Display hostname

host

Other network commands

ifconfig
iwconfig
iwlist scan
ifup interface
ifdown interface

Boot

Turn off the system in secure way.

shutdown <arguments>
-a Use /etc/shutdown.allow
-t <sec> Tell init(8) to wait <sec> seconds before sending kill signal
-k Only send shutdown warning without really shutting down
-r Reboot
-h Halt after shutdown
-f Skip fsck on reboot
-c Cancel running shutdown
-F Force fsck on next boot
<time> Shutdown at <time>
<warning message> Display <warning message> along with the shutdown warning to all logged in users

Search for files, for patterns

grep [pattern] [files]

Search for [pattern] in directory

grep -r [pattern] [dir]

Search for [pattern] in the output of [command]

[command] | grep [pattern]

Find all files with filename

locate filename

File Permissions

Change permissions of file to [octal]

chmod [octal] [filename]

Change permissions of file to rwx for all

chmod 777 filename

Change permissions to rwx for the owner, rw for group and world

chmod 755

System Commands

Show the current date and time

date

Print current date and who’s online

date;who

Set timezone

timedatectl set-timezone America/Chicago

Show a small calendar

cal

Show the system’s current uptime

uptime

Display a list of online users

w

View your login name

whoami

Show kernel information

uname -a

Show CPU information

cat /proc/cpuinfo

Show memory information

cat /proc/meminfo

Get manual for [command]

man [command]

Display disk usage

df

Display directory space usage

du

Show memory and swap usage

free

Show possible locations for [app]

whereis [app]

Display which [app] will be run by default

which [app]

Display active processes

``bash ps


Kill process ID [pid]

```bash
kill [pid]

Kill all named [proc]

killall [proc]

Linux Swap File

/etc/dphys-swapfile

Restart swap service

/etc/init.d/dphys-swapfile stop
/etc/init.d/dphys-swapfile start

Text Editors

emacs
pico
vi
nano

To convert nmap xml to html

xsltproc nmap-output.xml -o nmap-output.html

MYSQL

mysql -u root -p[root_password] [database_name] > dumpfilename.sql
mysql -u root -p[root_password] [database_name] < dumpfilename.sql

Others

Write ISO image to drive

sudo dd bs=512M if=file.iso of=/dev/sdX

User ID info

id -u username <-- get user uid
id -g username <-- get user gid
id -G username <-- get groups user belongs to

Cisco ASDM Java Web Start

javaws https://ip.of.asa:port/admin/public/asdm.jnlp