Linux How to Find File by Name

Linux How to Find File by Name

To search for files on the disk, you can use the find command. The find command has the following syntax:

find /where_to_start -name "name_of_file"

If you do not mention the parameter /where_to_start, it will automatically search in the current directory. The current directory, which you currently stand, is available by typing pwd command. The second parameter -name “name_of_file” – is shown filter. This filter shows only those files in which there is a string “name_of_file”. You can also include an asterisk, for example: “name_of_file.*”.

If you want to recursively overview /etc directory and find all files that have the extension “.conf”, you can do it this way:

find /etc -name "*.conf"

If the find command does not access to any folder, it write error about it. If you do not run the command as super user, it is better to redirect error messages to $HOME/find_errors or to trash /dev/null.

In next example we redirect errors to file find_errors that will be situated in our home folder:

find /etc -name "*.conf" 2> $HOME/find_errors

In next example we redirect errors to the system trash:

find /etc -name "*.conf" 2> /dev/null

To find file by a name, ignoring the case use option -iname:

find /etc -iname "name_of_file"

If you want to find all files these don’t match the pattern:

find /etc -not -name ".*.conf"

JOE Tutorial for Linux

JOE Tutorial for Linux

Start joe:

joe ./textFile.txt

Show help:

Ctrl + K + H (to hide help, enter this command again)

Save file

Ctrl + K + D

Save and exit:

Ctrl + K + X

Exit without saving:

Ctrl + K + Q

Go to previous/next screen:

Ctrl + U/Ctrl + V

Move cursor to the start/end of file file:

Ctrl + K + U/Ctrl + K + V

UnDo recent change:

Ctrl + Shift + _

ReDo undone change:

Ctrl + Shift + ^

Insert or overwrite:

Ctrl + T

Selecting text:

Ctrl + K + B (block begin)
Ctrl + K + K (block end)

When you have block selected, to move block using:

Ctrl + K + M

Delete block:

Ctrl + K + Y

Copy block:

Ctrl + K + C

Search in the file:

Ctrl + K + F

Then choose between ignore (I), replace (R) options. To navigate to next result use:

Ctrl + L

If you want to change joe’s default setting use:

Ctrl + T

You can turn on/off auto-indent, word wrap, line numbers, highlighting and set tab width, left margin, etc.

Linux How to Change Password

Linux How to Change Password

If you want to change password of user that are currently logged in, just type:

passwd

Input old password, then type new password 2 times.

Did you run the passwd command as root? There is a difference between whether you change the password as an root or as a standard user. While the root is called upon to enter a password, the password policy more fully. But if he fails, the password is changed, even though the system grumble. Current user must comply with password policy.

Let’s assume, that you are user bob. If you would like to change bob’s password, log as bob and type:

passwd

If you are root, and if you want to change bob’s password, type:

passwd bob

Please, always try to check, if you set good new password. There is a possibility, that you misspell the password, so try to log in to second console (or putty session) immediately after login.

You can also set the following options:

Option Property
-d Delete password for an account. It will set a passwordless account.
-e Expire. The user will be forced to change the password in the next login.
-n Minimum password lifetime.
-x Maximum password lifetime.

 

Linux How to Zip

Linux How to Zip

To create a compressed file and save disk space use following the following syntax:

zip options name.zip file1 file2 file3 folder

The zip program compresses one or more files into the name.zip archive. Zip has options from “1” to “9”. Option -1 use for fast compression and -9 for better compress ratios.

zip -9 name.zip file1 file2

If you want to insert a new file to the existing zip archive use options “u” which means update.

zip -u name.zip newFile

To decompress the zip archive use command:

unzip name.zip

To decompress to a specific directory use the “d” option:

unzip name.zip -d /directory

Command tar is a great tool too. You can compress files with tar:

tar -zcvf name.tgz file1 file2 file3 folder

To unzip compressed file type:

tar -zxvf name.tgz

NANO Tutorial for Linux

NANO Tutorial for Linux

Start nano:

nano textFile.txt

Show help:

Ctrl + G

Hide help:

Ctrl + X

Save file:

Ctrl + O

Save and exit:

Ctrl + X then answer Y

Exit without saving:

Ctrl + X then answer N

Go to previous/next line:

Ctrl + P/Ctrl + N

Go to previous/next screen:

Ctrl + Y/Ctrl + V

Go to the line and column number:

Ctrl + _

Insert another file into the current file:

Ctrl + R, enter path to the file

Search for string or regular expression:

Ctrl + W

Cut the current line and store it in the cutbuffer:

Ctrl + K

Uncut from the cutbuffer to the current line:

Ctrl + U

Copy the current line to cut buffer:

Alt + 6 or Alt + ^

How to Install Software in Linux

How to Install Software in Linux

Shared libraries

Print command dependencies of shared libraries

# ldd /bin/ls

Add new shared library to program icq

# ldconfig -n /opt/icq/lib

Actual shared library in cache (/etc/ld.so.cache)

# ldconfig -p

Get settings of shared libraries

# cat /etc/ld.so.conf.d /etc/ld.so.conf.d/*

RPM Package Manager (RPM)

Install

# rpm -i package.rpm

Uninstall (erase)

# rpm -e package.rpm

Upgrade

# rpm -U package.rpm

Upgrade only if an older version is installed

# rpm -F package.rpm

Print list of all installed packages

# rpm -qa

Find verbose information about package

# rpm -qv package

Print information about a package

# rpm -ql package

Display only the config files in package

# rpm -qc package

Display install destination folder before installation

# rpm -qpl package.rpm

Print changelog before installation

# rpm -qp --changelog package.rpm

Print name of package which binds to file

# rpm -qf /etc/exports

Integrity verification of all packages

# rpm -Va

Debian Package (dpkg)

Install

# dpkg -i package.deb

Uninstall (remove)

# dpkg -r package

Uninstall (include config files)

# dpkg -r --purge package

Display package’s files content

# dpkg -c package.deb

Display status of specified package

# dpkg -s package.deb

List files installed to your system from the package

# dpkg -L package

Print partially installed packages and give suggestions on how to fix them

# dpkg -C package

List of all installed packages

# dpkg -l

Source code

Extraction

# tar –xzf /path/name/package.tar.gz

# tar –xjf /path/name/package.tar.bz2

Prepare for compile

# ./configure

Start compile

# make

Install

# make install

Linux: How to Connect External Hard Drive

Linux: How to Connect External Hard Drive

Diagnostic Commands

Display from log directory what was recently connected

# tail -20 /var/log/messages

Display list of the partition tables

# fdisk -l

Display device parameters

# hdparm /dev/sda

More detailed information

# hwbrowser

# more /etc/sysconfig/hwconf

Connection Process

Run fdisk

# fdisk /dev/sda

Get list of existing partitions

Command (m for help): p

Delete partition which exist on the drive

Command (m for help): d

Create new partition

Command (m for help): n

Verification before save

Command (m for help): p

Save new table to drive

Command (m for help): w

Update partition table in system

# partprobe

Alternative to partprobe is hdparm -z

# hdparm -z /dev/sda

Format created partition to ext3 file system

# mkfs.ext3 /dev/sda1

Verification if the partition is not mounted

# df -hT

Mount new-created partition to directory /mnt/usb. If a directory does not exist, first created it.

# mkdir /mnt/usb

# mount /dev/sda1 /mnt/usb

# df -hT

Display disk partitions

# parted /dev/sda unit GB print free

Linux How to Generate Random Password

Linux How to Generate Random Password

Generating password is relatively simple and can be done in different ways. You can install tools to generate passwords, a few examples:

  • mkpasswd (Debian/Ubuntu)
  • makepasswd (Debian/Ubuntu)
  • pwgen (CentOS/Fedora/RHEL)

When you don’t have installed these tools, here are useful commands:

date +%s | sha256sum | base64 | head -c 32 ; echo

We used time in seconds as a input to hash function sha-256 and print first 32 chars. You can replate sha256sum with other hash function (md5sum).

strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 32 | tr -d '\n'; echo

/dev/urandom is the built-in feature which generate random chars.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32; echo

Another way using /dev/urandom, even simpler.

You can also create the script:

generatePasswd () {
local l=$1
[ "$l" == "" ] && l=16
tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}

generatePasswd "$1"

Linux How to Change Hostname

Linux How to Change Hostname

Hostname identifies a computer on the network and it is visible in the terminal. We set up hostname during the installation operating system, but sometimes we want to change it.

Use command “hostname” to change hostname:

hostname new_hostname

Will set the hostname of the system to new_hostname. Open a new terminal to verify change hostname.

If you want to change the hostname permanent use the command “sysctl kernel.hostname” to change it:

sysctl kernel.hostname=NewHostname

Output: kernel.hostname = Newhostname

Command hostname without any parameter prints the current hostname of the system.

Linux How to Extend Swap Space

Linux How to Extend Swap Space

Display swap usage summary

# swapon -s

Show how many space swap needs in megabytes

# free -m

Create new swap disk.

# fdisk /dev/sdb

Format new disk to swap format

# mkswap /dev/sdb1

Add new part of swap

# swapon /dev/sdb1

Verify if there is more space in swap space

# free -m

Append next line to file /etc/fstab.

/dev/sdb2 swap swap defaults 0 0

If you need old swap use

# swapoff /dev/sda6

Linux How to Execute Script

Linux How to Execute Script

There are many ways how to execute a script, the first one is to execute the script by specifying the interpreter:

bash script.sh
sh script.sh

For debugging use option “-x” to see what is being done.

bash -x script.sh

As an interpreter, you can use sh, ksh, csh, bash etc.

If you want to execute the script without specifying an interpreter, you need to set execute (+x) permission:

chmod +x script.sh

Then you can execute the script following way:

./script.sh

Another way how to run the script:

source script.sh

The script doesn’t need to execute permission in this example. If the script makes any changes to the environment, it will be visible after running the script, because commands in the script are executed in the current shell.