Technohelp

Linux OS

Linux Commands

Useful commands for getting things done in Linux based operating systems


Updated at: 2024-05-12 19:32:37 (Author: Admin User)

Silence Command line errors

After the command, type the following:

2>/dev/null

Add user to a group

Current user (using 'adduser' command):

sudo adduser $USER www-data

Different user (using 'usermod' command):

usermod –a –G GroupName UserName

Note: usermod may only work if switching user to root first.

chown

sudo chown -R www-data:www-data /home/$USER/public_html

cp

The cp command makes a copy of a file for you. For example, type:

cp file foo

..to make a exact copy of file and name it foo, but the file file will still be there.

Directory size search

List sizes of subdirectories in size order, suppressing errors

du -sh <path>/* 2>/dev/null | sort -h

Make new files inherit group permissions

chmod g+s /parent/path

 

Set Default Permissions for all Files and Folders in Directory

NOTE: See 'Make new files inherit group permissions' section above.

Set default permission for group and other

setfacl -d -m g::rwx /<directory>

setfacl -d -m o::rx /<directory>

See: How to Set Default File Permissions for All Files, Folders

find

Find file or files recursively in the current directory:

find . -name testfile.txt

Find files recursively in a target directory:

find /<directorypath> -name *.jpg

More information:

Find Files in Linux using the command line

grep

Wildcard Directory Search for content of file

grep "[content]" *

Recursive case-insensitive directory search for content of file in directory

grep -ri "[content]" [directory]

groups

Check group membership of a user

groups [username]

list members of a group

getent group <groupname>

Search for user in various groups

getent group| grep <username>

Create Symlink

This command creates a symbolic link. Example as follows:

ln -s /path/to/original /path/to/link

ls

The ls command shows you the files in your current directory. Used with certain options, you can see sizes of files, when files where made, and permissions of files. For example, typing

ls ~

will show you the files that are in your home directory.

Get Linux version information

To see the latest version of (Ubuntu / Debian) Linux, enter the following command

lsb_release -a

mkdir

The mkdir command will allow you to create directories. For example, typing:

mkdir music

will create a music directory in the current directory.

mv

The mv command moves a file to a different location or will rename a file. Examples are as follows:

mv file foo

Note:The command above will rename the file file to foo.

The next command will move the file foo to your Desktop directory but will not rename it. You must specify a new file name to rename a file:

mv foo ~/Desktop

Following command moves the 'realname' folder into the 'modules' folder in a Drupal cms installation (/var/www/drupal/sites/all): mv realname modules/

netstat

The command below shows ports open (listening) on a linux machine

netstat -an | grep "LISTEN "

pwd

pwd: The pwd command will show you which directory you're located in (pwd stands for “print working directory”). For example, typing

pwd

in the Desktop directory, will show ~/Desktop.

rm

Use the rm command to remove or delete a file in your directory. It will not work on directories which have files in them

sudo rm -r [target directory] : This command can remove the target folder AND all files therein....

File and Folder Permissions

When using the linux terminal screen to set file permissions, it important to note that all files and folders are simply classed as files and the process for setting permissions on these objects works no matter what type of file you are working on.

The example below changes the owner of the /var/www/phpTest to the user administrator:-

sudo chown administrator /var/www/phpTest

The next example changes a jpeg file in a folder so that both the www-data user and group is the owner

sudo chown -R www-data:www-data [filename]

Viewing permissions of a file

The following command lists the permissions of the files contained in the web server directory (/var/www) on the linux web server

ls -l /var/www

Changing permissions on a file

the 'chmod' command is used to change permissions on files (as in files and folders) in a Linux terminal window.

The following command assumes that the user has changed directory on a linux terminal to a directory where the 'index3.php' file resides. The command below enables other users (o) to write (w) to the file in addition to administrator user who already has permissions:-

chmod o+w index3.php

Table - chmod usage:

Options Definition
u owner
g group
o other
x execute
w write
r read
+ add permission
- remove permission
= set permission

 

Create and ISO from CD or DVD

dd if=/dev/cdrom of=/home/username/iso-name.iso

Replace file name in 'of=' part of the command with the destination and file name of your choice.

Terminal Window specifics/navigation

CTRL-C : Stops a program in the linux Terminal Window

CTRL-Z : Halts a program in the linux Terminal Window but leaves it running