Tag: bash

By Rand Whitehall

damn small linuxIf you want to move a file from one place to another, use the mv, or move command. Now, the mv tool can send a file from one directory to another, but it also can rename a file. If you simply want to change the name of a file, say, from joo.txt to joe.txt, you can do that with mv also. Here are a few examples to get you going.

mv joe joe1

The file joe is simply renamed joe1

mv joe /Documents/joe

The file joe is moved to the Documents directory.

Now, what if we wanted to move joe.txt to the Documents directory and at the same time rename it to joe2?

mv joe Documents/joe2

This would move the file joe to the Documents directory and would change the name to joe2. This is similar to cp, but the original file is changed. With cp we get a new file and the original file is unchanged.

If you’d like to see the contents of a file, use the cat command. cat stands for concatenate. cat will display the contents of a file and also join, or concatenate several files.

cat joe

This command will output the contents of joe.

What if you’d like to view the contents of two or more files?

cat joe bob

Will output the contents of joe and then bob.

Ok. Now let’s play around with cat and two new commands: touch and echo.

Do this:

touch jj.txt

Which will create new text file called jj.txt.

echo “Hi there” >> jj.txt

This adds the text “Hi there!” to jj.txt.

You can append some more text to the end of jj.txt with another echo command and two greater than signs “>>” like this:

echo “How are you?” >> jj.txt

Now take a look at the contents of jj.txt via:

cat jj.txt

One thing to know when using cat, if you use only one greater than sign “>”, it will overwrite the contents of the file. Be careful!

Let’s overwrite jj.txt on purpose.

echo “See you later.” > jj.txt

Now view the contents via cat:

cat jj.txt

… and you should see only the “See you later.” line.

I hope this helped you become a little more familiar with the Linux command line. Have fun and experiment. Remember, while playing around, it’s best to create a new directory and make new files specifically to experiment with so you don’t lose any important data. Soon you’ll be using the command line like a pro!

Rand writes about web design, men’s health and latex free nitrile gloves. Please check out his new website all about Blue Nitrile Exam Gloves for info and nitrile glove knowhow! Rand’s other writings can be found here: Rand Whitehall.

Tags: , , , , ,

By Rand Whitehall

To copy a file in the Linux command line interface (cli) then use the cp command. If you are familiar with DOS, cp is like the COPY command. To copy a file you need to write the name of the file you want to copy and the name of the newly copied file.

So, to make a copy of my file called joe and name the new file joe2, I type:

cp joe joe2

I then issue the ls command to show a list of the current files in the directory and I should see my old file joe and a new file called joe2 which is an exact copy of tom.

Now what if I wanted to copy the file joe, but put the copy in another directory? Well, then I simply specify which directory I want the copy to go into.

cp joe Documents/joe2

This will copy the file joe, name the new copy joe2, and place joe2 in the Documents directory. So with one simple command I copied, renamed, and moved a file. It took under a second. If I had done that in the GUI (graphical user interface) it would have taken at least a minute and a bunch of clicks.

What if I type this?: (hint: Documents is a directory.)

cp joe Documents/

Well, if you said a new copy of joe (named joe) would be created in the Documents directory, then you’d be right.

Since we did not specify a name for our copy, but did specify a directory, cp simply used the original name.

What if we do this?:

cp bashcp joe

The output is this an error:

cp: missing destination file operand after `joe’

Try `cp –help’ for more information.

Oops. cp needs a destination, which is either a new name for the copied file, or another directory to place the copy into.

I hope this helps you understand the basics of the GNU Linux command cp. The more you learn about the different cli commands, the more you can do. Soon you’ll be saving tons of time using the cli to perform tasks that would have taken the GUI hundreds of clicks.

Please be careful, though, when starting out with the cli. The command line tools can perform just about any task quickly and efficiently. But it’s easy to damage your system if you accidentally delete something or move a file that shouldn’t have been moved. The cli is like a very sharp katana that can slice through just any problem you may have, but it can also do unintended harm if you aren’t careful.

So when starting out, it’s best to work in safe, “sandbox” directories, on files you have created specifically to learn cli commands. I usually make a new directory, then copy a few files in to play with, so if I screw up, it’s no problem.

Rand writes about web design, men’s health and nitrile gloves. Please check out his new website all about allergy free Nitrile Gloves for info and nitrile glove know how! Check out the Nitrile Gloves vs. Latex Gloves page to find out how nitrile stacks up against latex.

Tags: , , ,

By Rand Whitehall

If you are just starting out using the command line in Linux, knowing just a few key commands can take you a long way towards fluency in the cli (command line interface) environment. Most new linux users fear the dreaded command line and its archane text-based commands. But if you warm up to the command line slowly and patiently, you’ll start to see you can wield great power over your computer using cli tools. It is truly amazing how much you can do with the cli and how quickly and efficiently it can do things. Some tasks can be done with a few lines of commands in under a second that would take thousands of mouse clicks and hours to accomplish in a typical GUI (graphical user interface).

The ls command is very powerful and useful so we’ll start there, then take a look at the file command.

ls: The ls command lists the contents of a directory. Simply type ls and hit the enter key. You should see a list of the contents of your current directory.

Now, if you want to get a bit fancy, you can tell ls to show you file sizes as well by adding the -l switch like this: ls -l.

This will show you the contents of the directory and files size, file date, file time and file name.

Now, while you are looking at details of the files in your current directory, you may be wondering what type of files they are. For example, if my home directory had a file called joe, I would type file joe to find out what type of file it was.

The file named joe is a text file so the file command outputs: joe.txt UTF-8 Unicode English text.

I’ve also got a directory listed called bob. If I type file: bob, then file outputs: bob/ directory.

The file command can tell us about any type of file. Digging deeper into my filesystem I come across a file that I don’t know. What type of file is it? Run the file command. file: bak.sh. The output of the file command: Bourne-Again shell script text executable. Ahh, it’s a bash script.

What’s a bash script? It’s a list of cli commands strung together in a text file. A bash script is like a small program that can perform just about any number of tasks. Very powerful stuff once you get a few more commands under your belt.

Rand writes about Linux, old school safety razors and blue nitrile gloves! Check out Rand’s site all about latex free nitrile gloves, a great barrier against infection. Black Nitrile Gloves are also available and used often by tattoo artists, mechanics and industries where keeping dirt and grime free is important.

Tags: , , , , ,

By Josh Bellendir

This tutorial will help the novice Unix/Linux user to be able to set the Path Variable. This can be useful to speed up access to software in the linux environment. Once you have a path added to your path variable, you can then simply type the executive to run the program or filename to open the file. You won’t need to specify the full path any longer.

To view what your current PATH variable is type the following at the command prompt:

# echo $PATH

This will output the PATH stored in the $PATH variable. If you simply wish to add an additional directory, simply do something like this:

# export PATH=$PATH:/the/directory/you/want/to/add

Then type echo $PATH to view the results and to make sure everything worked out. However, you should know that this is only temporarily set for the current terminal/instance you are logged into. If you want this path to always be set then you will want to edit the.bash_profile file which should be located in your home directory. Edit the.bash_profile with your favorite editor. For example:

#vi /home/myusername/.bash_profile

Find the line that states PATH=$PATH:$HOME/bin or something similar. And then just add whatever directory you want to have included in the path. For example:

PATH=$PATH:$HOME/bin:/usr/local/bin:/export/mycustomerapps

Save the file and you’re done. The next time you boot up your Linux box or create a new terminal connection, you will have your path set.

Written by Josh R Bellendir, 12/31/2010
For more articles, stories, tutorials, and reviews like these, please check out http://www.jbellendir.com.

Tags: , , , , , , ,

By Erick M Aqqa

Unix based operating systems like Linux offer a unique approach to join two discrete commands, and generate a new command using the concept of pipe(lines). For example, consider command1|command2. Here, whatever output is generated by the first command becomes the standard input for the second command. We can develop more and more complex Unix command sequences by joining many commands while maintaining input output relationships.

Another more Linux specific example would be ls -l|grep “^d”. This command displays details of only directories of the current working directory, i.e. the output of the ‘ls -l’ command becomes the input to the grep command, which displays only those lines that start with ‘d’ (they are nothing but the details of the files).

ls -l | grep “^d” | wc -l

This command displays number of directories in the given file.

grep “bash$/ etc / passwd | wc -l

This command displays number of users of the machine whose default shell is bash.

cut -t “: “-f 3 / etc / passwd | sort – n | tail – l

This command displays a number which is the largest used UID number in the system. Here, cut command first extracts UID’s of all the users in the system from the /etc / passwd file, and the same becomes input to sort; which sorts these numbers in numerical order and sends to tail command as input which in turn displays the largest number (last one).

tee command

The ‘tee’ command is used to save intermediate results in a piping sequence. It accepts a set of filenames as arguments and sends its standard input to all these files while giving the same as standard output. Thus, use of this in piping sequence will not break up the pipe.

For example, if you want to save the details of the directories of the current working directory while knowing their using the above piping sequence we can use tee as follows. Here, the file xyz will have the details of the directories stored.

ls -l | grep “^d” |tee xyz | wc -l

The following piping sequence writes the number of directories into the file pqr while displaying the name on the screen.

ls -l | grep “^d” | tee xyz | wc -l |tee pqr

cmp command

The cmp utility compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same. If they differ, the byte and line number at which the first difference occurred is reported.

Bytes and lines are numbered beginning with one.

For example, cmp file1 file2

comm command

comm is a command used to compare two sorted files line by line.

Compare sorted files LEFT_FILE and RIGHT_FILE line by line.

-1 suppresses lines that are unique to the left file.

-2 suppress files that are unique to the right file.

-3 suppress lines that appear in both the left file and the right file. For example, comm p1 p2.

A pipe thus helps connect a set of processes, so that the output of one becomes the input of another. It lets a user browse through a large amount of data in a convenient manner.

Linux2Aix is an upbeat Linux blog containing all the latest and the newest Linus news and how-to’s for both amateur and professional Linux lovers

Tags: , , , , , , , , , , , , ,

The Complete Steps to Create and Run a Linux Script to Run Linux Commands
By Clyde E. Boom

Creating and running a Linux script to automatically run a series of Linux commands that you commonly run is easy!

With a Linux script, you put a series of routinely run commands in a text file, and then run all of them by simply typing in the name of the file and pressing Enter.

Linux Script Example to Create a Script to Automatically Run A Series of Linux System Administration Commands2455513753_282aa586b7

Follow along with the steps in the example below to create and run your first Linux script!

1. Run a Linux text editor.

2. Put the following text at the top left of the text file (indented below for emphasis):

#!/bin/bash

This indicates that the text file is a Linux script file.

Press Enter twice to have a blank line below the line above.

3. Put the Linux command(s) in the script file.

The Linux commands below are used to provide an example. You can put any commands in a script.

The Linux commands below will: clear the screen, change into the /etc directory path, and then show the current path with the Linux pwd (path to working directory) command.

Then provide a long list of the fstab file (to show you that it’s there) and then change into your home directory (represented by the ~ symbol) and then show the path of the current directory.

The Linux echo command is not required, but has been put in the file to show the progress of the execution of the script.

Also, you don’t need to indent the commands below in the Linux script – they are just indented here for emphasis.

clear
echo The screen has been cleared

cd /etc

pwd

echo This is the etc directory

ls -l fstab

echo This is a long listing of the fstab file

cd ~

pwd echo Now in my home directory

Linux Commands Training Tips: A Linux script can contain hundreds of lines of text if necessary – and also include complex programming logic, such as if . . . then statements.

4. Save the text / script file with a meaningful name to create it and by give it a name.

For example, if you want to list files in a few directories, call the file: listdirs

5. Run the Linux chmod command to change the permissions of the file and make the Linux text file “executable”.

In our example, the file is named: listdirs

Below is a Linux chmod command example for running the chmod command to change the permissions of the Linux script file – and to make the listdirs text / script file “executable”, so that you can run the script file in the same way as you run a command.

The $ (dollar sign) below is the Linux command line prompt. Don’t type in the $ (dolar sign), type in the command that appears at the right of the $ prompt.

$ chmod u+x listdirs

The Linux command above is chmod and it is being used to assign the x (executable) permission to the u (user) of the file with: u+x and the script file name is listdirs.

Running a Linux Script to Run System Administration Commands

To run a Linux script (that is in the “current” directory), such as the listdirs script, simply type in a period (dot) and a space and then the name of the file and press Enter.

$ . listdirs

The concepts and Linux command examples shown above work in Red Hat, Ubuntu, Fedora, Slackware, and Debian Linux – and also ALL Linux distributions.

By the way…do you want to learn exactly how to use Linux and run Linux commands for Linux System Administration and get real, practical Linux training experience by running hundreds of examples of Linux commands?

Just click to download my free new Linux commands training course book and Linux audio podcast (.mp3) files here: Linux Commands Training Mini-Course

Clyde Boom says “Learn how to use Linux commands with easy, self-paced Linux training materials that show you how to run hundreds of examples of the essential Linux System Administration commands – and get that new and better job, promotion, raise – or keep your current job!”

You can get your instant access to my free Linux commands training course at:
http://www.LinuxCommandsTrainingCourse.com

Tags: , , , , , , , ,

Important Concepts For Linux Beginners – Shells and Utilities
By Levi Reiss

A shell is the command interpreter program that serves as an interface between some users and 250px-Bash_demothe operating system itself. We say some users because most users rely on the graphical user interface. The Windows shell is the DOS command line interface accessed by clicking on Run and then entering the cmd command. The Windows graphical user interface is Explorer. This article describes the Damn Small Linux shell interface and several utilities, useful programs that may be launched from the shell. A subsequent article will describe the corresponding graphical user interface.

Why would anyone want to bother with a shell when the prettier, easier-to-learn and easier-to-use graphical interface is available? The answer is: It depends who you are and what you want to do. For system administrators or their associates it’s often much less cumbersome to use the shell rather than the graphical user interface. While Damn Small Linux commands may be quite arcane, they are often very powerful. And efficient. The Linux way of performing administrative and other technical tasks admittedly takes time to learn and master. But it does the job and does it well. In all fairness, many Windows systems administrators often apply command-line utilities. But they don’t have a powerful shell to help them do their work.

Historically Unix used the Bourne shell, the C shell which resembles the C programming language, and the Korn shell. Linux’s most widely used shell is Bash, also spelled BASH, the (Bourne-Again Shell). Damn Small Linux offers many shells but most people go with Bash both to communicate interactively with the operating system and to write programs known as shell scripts. If you program in Linux no matter which programming language you use you should learn some Bash specifics.

Utilities enable you to handle some very sophisticated processing. You can think of them as commands or as prewritten programs. Unix-Linux people often send the output of one command or utility to another command or utility for further processing. For example, the ps command displays active processes. It tends to generate voluminous output, especially in a busy system. Let’s say that you are interested only in the processes associated with a given terminal. You send (the technical term is pipe, expressed by the | character) the output of the ps command to the grep utility which looks for patterns within the input. You code a single line, multipart command to obtain the list of processes associated with that particular terminal. Unix and Linux are well known for elegant solutions. In contrast the Windows solution to this information need is much more clumsy.

The grep utility has many other uses including validating e-mail addresses. Let’s say that your web site asks potential subscribers to furnish their e-mail accounts when signing up for a newsletter. A sophisticated but relatively short statement coded in grep could validate e-mail accounts.

DSL-logoOther Damn Small Linux text processing utilities include the related egrep and fgrep commands, mawk a pattern scanning and text processing language, sed an editor that handles large files, and diff a utility that compares files. DSL provides utilities that compress and archive files, and a wide range of other utilities. If you need them, these Linux utilities can be very useful and time-saving.

Our next subject is Linux programming support.

Levi Reiss has authored or co-authored ten books on computers and the Internet. He loves the occasional glass of wine as exemplified by his wine websites including http://www.theworldwidewine.com. He teaches Linux and Windows operating systems plus other computer courses at an Ontario French-language community college. Visit his new website http://www.linux4windows.com which teaches you how to download and run Damn Small Linux on Windows computers, even if they are “obsolete.”

Tags: , , , , , , , , ,
Back to top