My Linux command notes
Directory/ File
List
ls {path} It's ok to combine attributes, eg ls -laF gets a long listing of all files with types.
ls {path_1} {path_2} List both {path_1} and {path_2}.
ls -l {path} Long listing, with date, size and permisions.
ls -a {path} Show all files, including important .dot files that don't otherwise show.
ls -F {path} Show type of each file. "/" = directory, "*" = executable.
ls -R {path} Recursive listing, with all subdirs.
ls {path} > {filename} Redirect directory to a file.
ls {path} | more Show listing one screen at a time.
dir {path} Useful alias for DOS people, or use with ncftp.
ln -s /var/www/wp-plugins/my-plugin my-plugin Creating a Symlink
Make, remove
mkdir {dirname}
rkdir {dirname} Only works if {dirname} is empty.
rm -r {dirname} Remove all files and subdirs. Careful!
Print working dir
pwd Show where you are as full path. Useful if you're lost or exploring.
Get folder size of all sub-folders
du -h --max-depth=1 {/path/to/folder}
df -sh get the disk size
Copy a file or directory
cp {file1} {file2}
cp -r {dir1} {dir2} Recursive, copy directory and all subdirs.
cat {newfile} >> {oldfile} Append newfile to end of oldfile.
Move (or rename) a file
mv {oldfile} {newfile} Moving a file and renaming it are the same thing.
mv {oldname} {newname}
Delete a file
rm {filespec} ? and * wildcards work like DOS should. "?" is any character; "*" is any string of characters.
ls {filespec} Good strategy: first list a group to make sure it's what's you think...
rm {filespec} ...then delete it all at once.
Permissions
- Unix permissions concern who can read a file or directory, write to it, and execute it. Permissions are granted or withheld with a magic 3-digit number. The three digits correspond to the owner, the group, and the world (everyone else).
Think of each digit as a sum:
- execute permission = 1
- write permission = 2
- write and execute (1+2) = 3
- read permission = 4
- read and execute (4+1) = 5
- read and write (4+2) = 6
- read, write and execute (4+2+1) = 7
chmod 600 {filespec} You can read and write; the world can't. Good for files.
chmod 700 {filespec} You can read, write, and execute; the world can't. Good for scripts.
chmod 644 {filespec} You can read and write; the world can only read. Good for web pages.
chmod 755 {filespec} You can read, write, and execute; the world can read and execute. Good for programs you want to share, and your public_html directory.
- You can also change file permissions with letters:
- u = user (yourself)
- g = group
- a = everyone
- r = read
- w = write
- x = execute
chmod u+rw {filespec} Give yourself read and write permission
chmod u+x {filespec} Give yourself execute permission.
chmod a+rw {filespec} Give read and write permission to everyone.
-
Change owners/groups
- To change all files/folders recursively in subdirectory myfolder
chown -R newowner:newgroup myfolder/
- To change owner only
chown -R newowner myfolder/
- To change group only
chown -R :newgroup myname/
Hardware information
-
lscpu
- The lscpu command reports information about the cpu and processing units. It does not have any further options or functionality.
-
lshw
- A general purpose utility, that reports detailed and brief information about multiple different hardware units such as cpu, memory, disk, usb controllers, network adapters etc. Lshw extracts the information from different /proc files.
- More commands in this context
hwinfo
- Hwinfo is another general purpose hardware probing utility that can report detailed and brief information about multiple different hardware components, and more than what lshw can report.
lspci
- The lspci command lists out all the pci buses and details about the devices connected to them.
The vga adapter, graphics card, network adapter, usb ports, sata controllers, etc all fall under this category.
lspci -v | grep "VGA" -A 12
- Filter out specific device information with grep.
lsscsi
- Lists out the scsi/sata devices like hard drives and optical drives.
lsusb
- List usb buses and device details. Use the verbose option
-v to print detailed information about each usb port
Inxi -Fx
- That script fetches hardware details from multiple different sources and commands on the system, and generates a beautiful looking report that non technical users can read easily.
lsblk
- List out information all block devices, which are the hard drive partitions and other storage devices like optical drives and flash drives
df
- Disk space of file systems. Reports various partitions, their mount points and the used and available space on each.
Pydf
- An improved df version written in python, that displays colored output that looks better than df
fdisk
- Fdisk is a utility to modify partitions on hard drives, and can be used to list out the partition information as well.
mount
- The mount is used to mount/unmount and view mounted file systems.
mount | column -t | grep exts
- Again, use grep to filter out only those file systems that you want to see
free
- Check RAM. Check the amount of used, free and total amount of RAM on system with the free command.
dmidecode
- The dmidecode command is different from all other commands. It extracts hardware information by reading data from the SMBOIS data structures (also called DMI tables).
/proc Files
- Many of the virtual files in the /proc directory contain information about hardware and configurations. Here are some of them
cat /proc/cpuinfo CPU information
cat /proc/meminfo Memory
cat /proc/version Linux/Kernel
cat /proc/scsi/scsi SCSI/Sata devices
cat /proc/partitions Partitions
sudo hdparm -i /dev/sda
- The hdparm command gets information about sata devices like hard disks.