Linux
Commands
chmod
Change the permissions of files or directories. For example, changing permissions on a ssh key file for the current user.
chmod 400 ~/.ssh/id_rsa
If you’re not logged in as root, use
sudo
when permissions denied. more info. Use this to help determine the octal value.
+x
is used to make a file executable.
chmod +x myshellscript.sh
chown
Change owner and group.
# change owner to gilfoyle
chown gilfoyle myshellscript.sh
# change owner and group to gilfoyle
chown gilfoyle:gilfoyle myshellscript.sh
cp
# copy the contents of a folder into an existing folder
cp -a /source/. /destination/
The -a
option is an improved recursive option that preserve all file attributes and also preserve symlinks.
The .
at end of the source path copies all files and folders including hidden ones.
find
# -iname for case-insensitive
find / -iname "*string*"
# . for current dir and sub dirs
find . -iname "*string*"
# output to "less" filter
find / -iname "*string*" | less
# use -type l for symlinks
find . -type l -ls
This example shows how to delete all node_modules
folders starting from the current directory with a preview first.
# list of directories to be deleted
find . -name 'node_modules' -type d -prune
# delete directories from the current working directory
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
This example will use a wildcard search for all files that contain “krita” and move them to a temp directory for archival or deletion.
# find and exec mv command
find / -iname '*krita*' -exec mv {} /home/gilfoyle/temp/ \;
Enter man find
for the manual.
hwinfo
Hardware information
hwinfo --short
ln
Use the ln -s
command to create a soft link (symbolic link). The syntax is the same for both file and directory symlinks.
ln -s /path/to/file /path/to/symlink
ln -s /path/to/dir /path/to/symlink
Create a soft link named myfile.sh
in the current directory to a file named myfile.sh in the /home/gilfoyle/source
directory. e.g.,
ln -s /home/gilfoyle/source/myfile.sh myfile.sh
Verify the link with ls -l myfile.sh
. The output should contain:
myfile.sh -> /home/gilfoyle/source/myfile.sh
The same syntax applies when creating directory of folder symlinks. Simply replace the file path with a folder path.
ls
List files and folders
ls
# show hidden files, e.g., filenames that start with a period
ls -a
# show files sizes in kilobytes, megabytes, etc., instead of raw bytes
ls -h
# show permissions
ls -l
lsof
List Open Files - list of all open files and the processes that opened them.
For example, to kill process when Error: listen EADDRINUSE: address already in use
First, list open files to get PID, e.g.,
lsof -i tcp:8888 ⬡ 10.21.0 [±webpack-5 ●]
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 21433 jim 20u IPv4 4917597 0t0 TCP localhost:8888 (LISTEN)
The you can kill the process. e.g.,
kill -9 21433
lshw
List Hardware - for example, use -C
/-class
to list by class of hardware.
sudo lshw -C network
Use lshw -short
to get a list of available hardware classes.
mkdir
Besides making single directories, create directory structures using the -p
flag.
For example, create example.com
with nested public_html
directory.
mkdir -p /var/www/example.com/public_html
In this example, create a src
folder with multiple nested folders using curly braces. e.g., js
and css
nested directories.
mkdir -p src/{js,css}
rsync
# copy the contents of a folder into an existing folder
rsync -a /source/* /destination
After the initial sync, it will copy only the files that have changed. You can use it over a network.
scp
Copy files and folders over ssh. For example, download home directory for user gilfoyle
from machine into the current directory.
scp -r gilfoyle@0.0.0.0:/home/gilfoyle .
Upload folder and its files recursively. For example, with the root account, upload extracted Drupal devel module to /var/www/modules
using port 8022.
scp -P 8022 -r devel root@0.0.0.0:/var/www/modules/devel
Download folder and its files recursively. For example, with the root account, download the devel module into the devel folder within the current directory using port 8022.
scp -P 8022 -r root@0.0.0.0:/var/www/modules/devel devel
ssh server
Install openssh server
apt-get install openssh-server
Unlocking the root account
usermod root -p password
# enter new password to unlock root
sudo passwd root
Check status
service ssh status
Configure openssh server
Using Nano
nano /etc/ssh/sshd_config
Allow root password login
# change
PermitRootLogin prohibit-password
# to
PermitRootLogin yes
Disallow root password login
# change
PermitRootLogin yes
# to
PermitRootLogin prohibit-password
Restart the service for any config changes to take affect
service ssh restart
stat
Retrieve information on files including permissions in both number and letter format.
➜ stat config
File: config
Size: 292 Blocks: 0 IO Block: 4096 regular file
Device: 2h/2d Inode: 7881299347901219 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-07-27 09:03:56.943000400 -0400
Modify: 2018-07-27 09:03:56.943000400 -0400
Change: 2018-07-27 09:25:51.771887000 -0400
Birth: -
sudo
Allows a user to run a command as another user, usually root. For a user to do this, they need to be added to the sudo
group.
usermod -aG sudo gilfoyle
Logout Ctrl + D and test new user sudo access. Substitute 0.0.0.0
with the server IP address.
ssh gilfoyle@0.0.0.0
# list files in the /root directory
sudo ls -la /root
After sudo
is enabled for the new user account, root password login can be turned off in the sshd_config file.
To prevent unable to resolve host
message when using sudo
, update the hosts file to contain the hostname for 127.0.0.1
. For example, what is after @ in the command line prompt. e.g., gilfoyle@vpshostname
.
sudo nano /etc/hosts
127.0.0.1 localhost vpshostname
tail
Monitor log files in real time. The -n300
argument show the last 300 lines. The default is only 10 lines.
tail -n300 -f error.log
Use the tail command with the
-f
argument to follow the content of a file. Use the-F
argument to follow the creation of a new log.
update
Download package lists from repositories and update them to get information on the newest versions of packages and their dependencies.
apt-get update
The Advanced Package Tool (apt) is primarily for Debian and Ubuntu Linux systems.
upgrade
Fetch new versions of packages existing on the machine if APT knows about these new versions by way of apt-get update
.
apt-get upgrade
Does the same job as apt-get upgrade
, plus it will also intelligently handle the dependencies, so it might remove obsolete packages or add new ones.
apt-get dist-upgrade
user
These examples use gilfoyle
for the username.
Add a new user.
adduser gilfoyle
watch
Will execute the command that follows every 2 seconds. e.g.,
watch date
To change the interval, use the -n
argument. For example, this will run the date
comand every 15 seconds.
watch -n 15 date
Resources
File and Folder Permissions
This page contains information on the series of letters and dashes that define file and folder permissions in Linux.
Java and Maven Install
Installing Latest Oracle Java and Maven. Optionally, you could use SDKMAN CLI and API for installing, switching, removing Java and/or Maven.