Showing posts with label command. Show all posts
Showing posts with label command. Show all posts

Friday, January 26, 2018

Linux Commands in a nutshell

Go back to the previous directory in shell
cd -
It goes back to the previous directory.
cd $OLDPWD
This can be also used in shell scripts.

Sunday, September 24, 2017

chmod permission list

Hi...

To see the bit of OS
getconf LONG_BIT 

Permission command(chmod):-

CommandMeaning
chmod 400 fileTo protect a file against accidental overwriting.
chmod 500 directoryTo protect yourself from accidentally removing, renaming or moving files from this directory.
chmod 600 fileA private file only changeable by the user who entered this command.
chmod 644 fileA publicly readable file that can only be changed by the issuing user.
chmod 660 fileUsers belonging to your group can change this file, others don't have any access to it at all.
chmod 700 fileProtects a file against any access from other users, while the issuing user still has full access.
chmod 755 directoryFor files that should be readable and executable by others, but only changeable by the issuing user.
chmod 775 fileStandard file sharing mode for a group.
chmod 777 fileEverybody can do everything to this file.

To unzip file


tar xvzf file-1.0.tar.gz - to uncompress a gzip tar file (.tgz or .tar.gz)

Resource Link: http://sabbirahamed.blogspot.com/2014/02/simple-usefull-linux-command.html

Friday, July 14, 2017

Commands, most commonly we use

Logging into the Server

1. ssh-keygen generate ssh keys

Even before we log in to the remote server, we generate a pair of public and private ssh keys. Ultimately, the contents of the public key *.pub can go into Github SSH keys or the remote computer's ~/.ssh/authorized_keys file.
$ ssh-keygen -t rsa -f project-name -C "name@email.com"

2. ssh secure shell

Next, to login there are various ways of using the ssh command. The 3 common ways that I have used are:
  1. with a config file at ~/.ssh/config
Host projectname
      HostName website.com
      Port 8000
      IdentityFile ~/.ssh/sshkey
      IdentitiesOnly=yes
      User username
  
And now we can ssh into the server with a much simpler command
$ ssh projectname
  1. ssh with a username and a public key
<!--emailoff-->$ ssh -i mykey.pem username@server.com<!--/emailoff-->
  1. ssh directly into a particular directory
$ ssh projectname -t "cd /path/to/app ; /bin/bash"

Transferring files

3. scp secure copy

Secure copy scp comes in handy for transferring files from the local machine to the remote machine and vice versa.
  1. from local to remote
<!--emailoff-->$ scp -i /path/to/key.pem /path/to/file username@server.com<!--/emailoff-->
  1. from remote to local
<!--emailoff-->$ scp -i ~/.ssh/key.pem  /path/to/source/file username@server.com:~/path/to/destination/file<!--/emailoff-->

4. cp copy

To copy files or entire folder:
$ cp /path/to/source-folder/filename /path/to/destination-folder
$ cp -r /path/to/source-folder/folder /path/to/destination
$ cp /path/to/source-folder/filename /path/to/destination . # dot: destination is current folder

5. mv move

To move files/folders is also the same as renaming.
$ mv /path/to/source-folder/filename . # move file to current directory
$ mv /path/to/source-folder . # move folder to current directory

6. echo output

To print out simple commands and piping the output to a new file.
$ echo hello
$ echo `date` >> currentime.txt # copy to file
$ echo `date` > currentime.txt # append

7. touch update a file

When used without any arguments, touch will simply create a file.
$ touch filename1 filename2 filename3

8. ln symlink

Symbolic links for files are useful when we want the exact file contents in 2 different places to be synced up. This can be handy for doing so with executable files if needed.
$ ln -s /path/to/source/filename /path/to/destination/filename

File contents

9. head top of a file

head is very useful to quickly view a certain first few lines of a file with an option -n to specify how many lines from the top.
$ head -n 5 /path/to/filename

10. tail end of a file

tail as the name goes, is the opposite of the command head and this gives the content of the last lines of a file. Using with the option -f, the output is appended as the file grows and this is used to continuously view server log files while changes are happening.
$ tail -n 5 /path/to/filename
$ tail -f log/production.log

11. cat see the contents of a file

cat is a simple way to view the file contents, but it can also be used to concatenate a few files into a new file.
$ cat /path/to/filename
$ cat file1 file2 >> newfile # newfile has contents of file1 and file2

Permissions

12. chmod change permissions

File permissions include the ability to read, write or execute the file for users owner, group or others. chmod can change these permissions with either a number or expression.
$ chmod u+x /path/to/script # user can execute
$ chmod 600 ~/.ssh/authorized_keys # user can read, write

13. sudo act as root

sudo gives the permission to run commands as the root user. It can be used for a single command or a series of commands. This command should definitely be used with caution as root user will have permissions to execute things a normal user might not.
$ sudo !! # execute the previous command with sudo
$ sudo cp /path/to/source /path/to/destination # execute as root
$ sudo su # switches from current user to root

14. chown own the folder/file

To quickly change the owner of a folder or file chown comes in handy invarious situations.
$ sudo chown -R username:username ~/path/to/folder

Directory

15. cd change directory

Changing directory is such a common command. Here are some ways to use cd:
$ cd ~ # go to home directory
$ cd - # go to the last visited directory
$ cd ../path/to/folder # go to relative directory one level up
$ cd /path/to/folder # absolute path

16. ls list files/folders

Another common commands is to list the directory contents. Using ls with 3 options are useful to view the dotfiles in long format to give a comprehensive look inside that folder.
$ ls -lah . # current folder
$ ls -lah ~ # home folder
$ ls -lah /path/to/folder # absolute path to a folder

Monitoring processes

17. ps running processes

Knowing the current processes running is another useful command through ps. Often, the list will be long. In this case piping the answer to a search terms is useful.
$ ps aux | grep unicorn # find all unicorn processes
The command ps aux will give a long list of useful information about the processes including the user, CPU usage, path to the command as well as the Process ID (PID) which will come in handy if we want to stop that particular process.

18. kill terminate a process

Often, we want to find that particular process and kill/stop it so that we can make some change and then run it again. Coupled with the previous command ps which will give the PID number, we can kill that particular process with a Unix signal 9. Here's an example:
$ ps aux | grep jekyll
$ sayanee  1835   0.0  0.8  2509028  68756 s003  S+    7:59PM   1:00.64 ruby /Users/command
$ kill -9 1835

19. lsof

At other times, knowing which process is running in a particular port is also useful. For this lsof comes in handy!
$ sudo lsof -i:8080 # complete info on the process
$ sudo lsof -t -i:8080 # just the PID of the process
$ kill -9 `sudo lsof -t -i:8080` # put the PID to kill it

20. crontab cron jobs for scheduling

Scheduling scripts to run at a certain time or an interval is helpful through CRON jobs. Sometimes CRON jobs are created through a backend application using the application specific variables. To see the output, listing the cron job is useful.
$ crontab -l

Wednesday, June 7, 2017

How to solve unmet dependencies?

macao@macao-pc:/tmp$ sudo apt-get install mongodb-org
Reading package lists... Done
Building dependency tree    
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 google-chrome-stable : Depends: libappindicator1 but it is not going to be installed
 mongodb-org : Depends: mongodb-org-shell but it is not going to be installed
               Depends: mongodb-org-server but it is not going to be installed
               Depends: mongodb-org-mongos but it is not going to be installed
               Depends: mongodb-org-tools but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).


Solution:


sudo apt-get install --fix-broken && sudo apt-get autoremove && sudo apt-get update 

Resource Link: https://askubuntu.com/questions/381894/problem-in-upgrading-linux-header-package-3-2-0-56-generic-pae-unmet-depend

Monday, June 5, 2017

How to set JAVA_HOME in linux?

Open /etc/environment in any text editor like nano or gedit and add the following line:

JAVA_HOME="/usr/lib/jvm/open-jdk"
(java path could be different)

Use source to load the variables, by running this command:

source /etc/environment
Then check the variable, by running this command:

echo $JAVA_HOME
Update

Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)

source /etc/environment

Resource Link: https://askubuntu.com/questions/175514/how-to-set-java-home-for-java

Another Section:

1. sudo update-alternatives --config java

2. sudo gedit /etc/environment
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
JAVA_HOME="/usr/lib/jvm/java-7-oracle"
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"

3. source /etc/environment

Wednesday, May 31, 2017

Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

First Process:


First catch the process containing the word apt. Then kill the process.

ps aux | grep apt
 
Then
 
sudo kill -9 processId.
 
 

Second Process:

 
You can delete the lock file with the following command:

sudo rm /var/lib/apt/lists/lock 
 
You may also need to delete the lock file in the cache directory

sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock 
 
After that, try opening Synaptic again.

Resource Link: https://askubuntu.com/questions/15433/unable-to-lock-the-administration-directory-var-lib-dpkg-is-another-process/315791#315791
 

Tuesday, May 30, 2017

Why dpkg is used ?

to do a configuring of all unconfigured packages if there has been a problem with an update and some packages' configuration is still pending:

sudo dpkg --configure -a
For a great tutorial that focuses on dpkg and apt-get, see chapters 5 and 6 of the Debian Handbook

Resource Link: https://askubuntu.com/questions/173465/what-is-dpkg-for

Monday, February 20, 2017

Gtk-WARNING **: cannot open display: xhost+ : How to Fix “Cannot Open Display” Error While Launching GUI on Remote Server

Question: When I try to launch any GUI application on a remote server, I’m getting the “cannot open display:” error, as shown below. How do I fix this?

For example, while launching the gedit on remote server, I got the following message.
(gedit:3658): Gtk-WARNING **: cannot open display: 

I get similar message when I try to open any GUI application. For 
example, launching Oracle Installer on remote server also gives the 
“cannot open display” error.

Answer: You can fix the “cannot open display” error by following the xhost procedure mentioned in this article.

1. Allow clients to connect from any host using xhost+

Execute the following command to disable the access control, by which you can allow clients to connect from any host.
$ xhost +
access control disabled, clients can connect from any host

2. Enable X11 forwarding

While doing ssh use the option -X to enable X11 forwarding.
$ ssh username@hostname -X

Enable trusted X11 forwarding, by using the -Y option,
$ ssh username@hostname -Y

3. Open GUI applications in that host

After opening ssh connection to the remote host as explained above, you can open any GUI application which will open it without any issue.
If you still get the “cannot open display” error, set the DISPLAY variable as shown below.

$ export DISPLAY='IP:0.0'


Note: IP is the local workstation’s IP where you want the GUI application to be displayed.

Thursday, February 16, 2017

Console/Command prompt output and error log write to a specific file

Output and error log write to a specific file.

 '>' will overwrite the file, '>>' will append to the file

Overwriting and appending output file
======================================

'>'  : for overwriting output file.
'>>' : for appending to the same logfile.



Overwriting and appending error log file
========================================

'2>' : for overwriting error log file.
'2>>': for appending to the same error logfile


java -jar test.jar > output.log

java -jar test.jar >> output.log

java -jar test.jar 2> error.log

java -jar test.jar 2>> error.log

Wednesday, February 15, 2017

How to run a jar file in command prompt?

First Solution

java -jar <myJar>.jar

Second Solution(Jon Skeet): 

You need to specify a Main-Class in the jar file manifest.
Oracle's tutorial contains a complete demonstration, but here's another one from scratch. You need two files:
Test.java:
public class Test
{
    public static void main(String[] args)
    {
        System.out.println("Hello world");
    }
}
manifest.mf:
Manifest-version: 1.0
Main-Class: Test
Note that the text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
Then run:
javac Test.java
jar cfm test.jar manifest.mf Test.class
java -jar test.jar
Output:
Hello world

Resource Link:

  1. http://stackoverflow.com/questions/5774970/run-jar-file-in-command-prompt
  2. http://stackoverflow.com/questions/1238145/how-to-run-a-jar-file

Monday, January 9, 2017

Wednesday, October 26, 2016

Linux chown - Be careful

The chown command is most commonly used by Unix/Linux system administrators who need to fix a permissions problem with a file or directory, or many files and many directories.

If you're not the root user on your Unix or Linux system -- or even if you are -- be very careful with the chown and chgrp commands. Once you grant ownership of files to another user on your Unix/Linux system, guess what? You don't own them any more. This can lead to all sorts of nasty problems, such as not being able to delete the files.
Fortunately the chown and chgrp commands are commonly used by Unix system administrators (Unix 'root' users) and this isn't usually much of a problem, but I thought I better warn you about it anyway, as it can create a mess.

Wednesday, September 21, 2016

Copy file local to server


You can copy your files to a folder you can access... like folders inside /home/ubuntu/, then do an 'mv' to copy files from there to your webapps folder


sudo cp /home/user/report001.xml /opt/sl/ja/report001.xml

  1. How to transfer files to server? WinSCP gives permission denied
  2. http://unix.stackexchange.com/questions/188285/how-to-copy-a-file-from-remote-server-to-local-machine

Tuesday, August 30, 2016

SoftLink:How to create a link to a directory

How to create a link xxx to /home/jake/doc/test/2000/something/?
Assume the xxx is created under /home/jake and you're currently in /home/jake. When you do cd xxx, you directly go to /home/jake/doc/test/2000/something/.
Ans:
Symbolic or soft link (files or directories, more flexible and self documenting)
#     Source                             Link
ln -s /home/jake/doc/test/2000/something /home/jake/xxx
Hard link (files only, less flexible and not self documenting)
#   Source                             Link
ln /home/jake/doc/test/2000/something /home/jake/xxx
More information: man ln
Resource Link

Thursday, September 18, 2014

Remove file, folder and lock of folder using linux command


Title : Remove file, folder and lock of folder using linux command

How to remove all files from a folder(folder name=eclipse) ?
sudo rm -rf eclipse/*

How to remove a folder (folder name = eclipse)?
sudo rm -rf eclipse

Install Java update version from this page.

Uninstall Java from ubuntu

Install eclipse

For removing lock of a folder or file access(I want to access all files from the folder named by .ssh)
sudo chmod -R 777 /home/pdm-rizvi/.ssh/