Linux Commands

History

How to clear the terminal command history

Clearing typed commands from terminal history: By default, up to the last 500 command lines a user types in the terminal window are saved into a hidden .bash_history file. The previously typed commands can be readily accessed by using the up and down arrow keys. This makes it easy to retrieve and reuse your recently used commands. However, maybe you want to clear the terminal command history list and start fresh? This simple tutorial explains the process of viewing and then optionally clearing the terminal history. Viewing the complete terminal command history: To view the complete history of commands typed in the terminal “for the logged in user”, open the terminal and type history How to clear the terminal command line history:

  1. Login with the user account whose terminal history you plan to clear
  2. Open a terminal window and type
    history -c
  3. Repeat the process if necessary for each user account

How to delete a single command from history on a Linux

  I‘m working in Ubuntu bash terminal application and remotely on a RHEL server in cloud platform. I typed the wrong and dangerous command. I no longer wish to remember dangerous command in the history file. How can I remove or delete a single command from bash history file?   You can use the history command to clear all history or selected command line.

How do I view history with line number?

Simply type the history command:

$ history

Sample outputs:

How to delete a single command number 1013 from history

The syntax is:

## Delete the bash history entry at offset OFFSET ##history -d offset   history -d number history -d 1013

Verify it:

$ history

How do I delete all the history?

The syntax is:
history -c

Tip: Control bash history like a pro

First, you can increase your bash history size by appending the following config option in ~/.bashrc file:


Save and close the file.

Where to find more information about history command?

    You can read bash man page by typing the following command:  
$ man bash  
Or simply type the following command:  
$ help history      

FIND

Find command examples

Let us try out some examples.

Finding files and printing their full name

You wish to find out all *.c (all c source code) files located under /home directory, enter: $ find /home -name “*.c” You would like to find httpd.conf file location: $ find / -name httpd.conf

Finding all files owned by a user

Find out all files owned by user USER: # find / -user USER Find out all *.sh owned by user USER: # find / -user USER -name “*.sh”

Finding files according to date and time

Files not accessed in a time period – It is useful to find out files that have or have not been accessed within a specified number of days. Following command prints all files not accessed in the last 7 days: # find /home -atime +7

  • -atime +7: All files that were last accessed more than 7 days ago
  • -atime 7: All files that were last accessed exactly 7 days ago
  • -atime -7: All files that were last accessed less than7 days ago

Finding files modified within a specified time – Display list of all files in /home directory that were not last modified less than then days ago. # find /home -mtime -7

Finding newer (more recently) modified files

Use -newer option to find out if file was modified more recently than given file. # find /etc/apache-perl -newer /etc/apache-perl/httpd.conf

Finding the most recent version of file

It is common practice before modifying the file is copied to somewhere in system. For example whenever I modify web server httpd.conf file I first make backup. Now I don’t remember whether I had modified the /backup.conf/httpd.conf or /etc/apache-perl/httpd.conf. You can use the find command as follows (tip you can also use ls -l command): find / -name httpd.conf -newer /etc/apache-perl/httpd.conf

Locate command

The locate command is often the simplest and quickest way to find the locations of files and directories on Linux and other Unix-like operating systems.   For example, the following command uses the star wildcard to display all files on the system that have the .c filename extension: # locate “*.c”    

Rename

Linux Rename File Command

  I‘m a new Linux user. How do I rename a file called resumezzz.pdf to resume.pdf using Linux bash command prompt?   You need to use the mv command. It is used to rename and move files and directories. The general syntax is as follows:  

mv old-file-name new-file-name mv [options] old-file-name new-file-name mv file1 file2

  In this example, the following command would rename a file called resumezzz.pdf to resume.pdf. Open a command-line terminal (select Applications > Accessories > Terminal), and then type:  

mv resumezzz.pdf resume.pdf

  If resumezzz.pdf is located in /home/user/docs/files directory, type:  

cd /home/user/docs/files mv resumezzz.pdf resume.pdf

  OR  

mv /home/user/docs/files/resumezzz.pdf /home/user/docs/files/resume.pdf

  Use the ls command to view files:

ls -l file1 ls -l file1 file2 ls -l /home/user/docs/files/*.pdf ls -l *.pdf   =============== NAMING

Linux rename a file syntax

In short, to rename a file:  

mv file1 file2

  You can get verbose output i.e. mv command can explain what is being done using the following syntax:  

mv -v file1 file2

  Sample outputs: `file1′ -> `file2′   ========   REMOVE

EXECUTABLE:   There are a couple ways. If you just want to do this temporarily, you can remove the execute bit from the file:

$ chmod -x /etc/init.d/varnish

Then re-add it when appropriate:

$ chmod +x /etc/init.d/varnish

The “official” way in Ubuntu (as well as in Debian and other Debian derivatives), though, is to use the update-rc.d command:

$ update-rc.d varnish disable    

Search and Find

SEARCH AND FIND files in locations

find / -xdev -name vip_monitor,sh  953  find / -xdev -name vip_monitor.sh  954  mv /root/vip_monitor.sh /root/vip_monitor.sh_

USE OF SH FILE AND EXTENSIONS

Run .bin file in Linux / UNIX

Change the permission of the file you downloaded to be executable. Type the following command: $ chmod +x file.bin Start the installation process or run .bin file. Type the following command: ./file.bin For example if .bin file name is application.bin. Type the following commands: $ chmod +x application.bin $ ./application.bin   Another example for Java Linux self extracting binary file: $ chmod +x jre-1_5_0-linux-i586.bin $ ./jre-1_5_0-linux-i586.bin

OR $ sh jre-1_5_0-linux-i586.bin    

Correct way to disable init-scripts to start at boottime on Debian

  In several articles and forums I see people telling the way of removing the init-script on a Debian system would be:   update-rc.d -f init-script-name remove   The more appropriate way is to disable the init-script from running at boottime:   update-rc.d init-script-name stop levels   In this way you prevent that in case you upgrade the package a new init-script will be created with the defaults. If the init-script is written with the defaults the init-script might start the process again at boot-up. If you disabled the init-script from running at boottime the init-script would not be recreated or overwritten with the init-script in the update.