Bash: Hardware Commands

4 minute read

Description:

Bash hardware commands that have to do with the file system and accessing files.

To move directories

1
2
3
4
5
6
7
   cd /path/to/go/to

   # Navigates to a user's "home". For root = /root/home or a user "gerry" is /home/gerry
   cd ~

   # Navigates to root of filesystem
   cd /

To view a directories contents (hidden and in list form):

1
   ls -al

To create a file

1
2
   touch filename
   touch mytext.txt

To copy a file (can use “.” as current path)

1
2
3
4
   cp soure/path destination/path

   # Copy file.txt from current directory to user's home directory
   cp ./file.txt /home/user/

To delete a file

1
2
3
4
5
6
7
8
9
10
   rm file.txt

   # Force remove a directory recursively. Be careful with this one!! 
   rm -rf 

   # Also run an echo first to make sure, example:
   echo rm -rf /home/user/docs

   #check output - looks safe; let's do it for real...
   rm -rf /home/user/docs

To move files

1
2
   mv /source /dest
   mv mytext.txt /home/user/documents/

To create a shortcut

1
2
   ln source destination
   ln mytext.txt /home/user/documents/

To create a folder

1
2
   mkdir foldername
   mkdir my-docs

To delete a folder (only if empty)

1
   rmdir my-docs

To mount drives in Linux

1
   mount (device name) /destination

To unmount drives

1
2
3
4
   umount /currentMountPoint

   # Unmount all drives
   umount /a

To Search for files # there are 3 ways to find files in Linux, “find” is the most aggresive

  1. locate => to use this, you need to run “updatedb &” first (use & to background it). This “db” is a built-in index of your file system. ex: locate mytext.txt

  2. which => which is used to figure out a programs dir. Ex: which nc.exe # should return /usr/share/windows-binaries/nc.exe or wherever you have nc.exe installed

  3. find => This is the most common. Tons of switches. Ex: find / -name mytext.txt

Archiving & Compression

Tar => The basic “zip” file for Linux systems. Commonly used with Gzip (see below).

To compress a directory to a tar.gz

1
   tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

To extract a tar to the current dir

1
   tar -xzvf archive.tar.gz

To extract a tarball into location where you want

1
   tar -xzvf backup.tar -C /location
  • NOTE: Only use z with gzip
  • Gzip => This program compresses the contents of files using complex mathematical algorithms. Files compressed in this way are given the extension .gz and need to be uncompressed before they can be used. To compress several files or even entire directories, use the tar command. The archive files created by tar end with .tar. If the tar archive was also compressed using gzip, the ending is .tgz or .tar.gz. If it was compressed using bzip2, the ending is .tar.bz2.

Viewing files:

To see a file’s information

1
2
   file filename
   file mytext.txt

To view a file in terminal

1
2
3
4
   cat filename

   # Use path to file
   cat ./mytext.txt
  • View a file half a page at a time:
1
2
   less filename
   less mytext.txt

To search within a file for a particular string

1
2
3
4
5
   # Searches files for your search string
   grep (search string)

   # Used often with pipelined input
   cat mytext.txt | grep info

To compare files

1
   diff file1 file2

File System Information:

To lists the disks

1
   fdisk -l

To see how much free disk space you have

1
2
3
4
   df -h

   # Human readable
   df -aT

To see how much used space you have (in current dir)

1
   du -h

To see how much free memory you have

1
2
   # Output in MB
   free -m

To see currently running programs memory usage

1
2
   # Press H for customization
   #### Top

To see a processes usage

1
2
   ps
   aux

To stop a process

1
2
3
4
5
   # You have to use -9 to force kill. This command requires that you know the process id first
   kill (process-id)

   # Allows you to specify name instead of PID
   killall (processname)

System Details

To Show system date

1
   date

To display PCI devices

1
   lspci

To see architecture of machine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   # To see lots of info
   uname -a

   # To see architechture
   arch
   uname -m

   # To display operating system
   uname -o

   #### To see the hardware platform
   uname -i

   # Network node hostname
   uname -n
   hostname

   # Show kernel version
   uname -r

   # for specific distro info:
   cat /etc/SuSE-release
   cat /etc/redhat-release

To see kernel messages

1
2
   dmesg | less
   dmesg | grep -i # (usb, memory, and many other options)

To see hardware info for attached devices:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   # CPU information
   cat /proc/cpuinfo

   # Show memory use
   cat /proc/meminfo

   # Show file swap
   cat /proc/swaps

   # Show version of the kernel
   cat /proc/version

   # Show mounted file systems
   cat /proc/mounts

Comments