Posted in databases, Unix, scripting

Unix – I did my best

1nnpyr

Hi Boys and Girls!

After my previous Unix-related post some of you complained that it was too short, to little knowledge and more meh than wow. This time I decided to do my best and fulfill all your Unix desires ūüôā

One of my biggest discoveries about Unix (and command line in general) is that
YOU HAVE TO KNOW.

You have to know where your file is.

You have to know what is the path to the folder in which your file would be stored.

You have to know how to do things.

In order to know things – I’ve gathered bunch of tips and commands that might be useful. ūüôā

At first – commands!

File management:

  • cat –¬†Concatenate and print files
  • chgrp –¬†Change the file group ownership
  • chmod –¬†Change the file modes/attributes/permissions
  • chown –¬†Change the file ownership
  • chattr – ¬†Change file attributes
  • cd ¬† – Change directory
  • cd – – Change to previous directory

(hint: cd without provided directory will take you back to home catalog – with the specified path will take you to that directory.

It is also possible to move up step by step or several parent directories at once, for example:

cd ../../../   Рwill move you 3 cataloges up)

  • df –¬†Report free disk space
  • echo –¬†Write arguments to standard output
  • file – ¬†Determine file type
  • find – ¬†Find files
  • gzip – Zip files
  • gunzip – Unzip files
  • ln¬† –¬†Link files
  • ls – List directory contents

(ls –color will list your files with selected color)

  • mkdir –¬†Make directories
  • more –¬†Display files on a page-by-page basis
  • mv –¬†Move or rename files

For examlpe Рif you wanna rename test1 file into test45 Рyour command would be like this:   mv test1 test45

  • pwd –¬†print working directory – Return working directory name
  • rcp – Transfer files to the remote host
  • rm – Remove files and catalogs
  • rmdir – Remove directories, if they are empty.
  • split –¬†Split files into pieces
  • touch –¬†Change file access and modification times
  • umask –¬†Get or set the file mode creation mask
  • unlink –¬†Call the unlink function

File system management:

  • badblocks – badblocks control
  • df – Report free disk space
  • dd –¬†Convert and copy a file

Process management:

  • at –¬†Execute commands at a later time
  • cron – Regular process run during¬†given timeframe.
  • fg –¬†Run jobs in the foreground
  • kill –¬†Terminate or signal process.
  • killall –¬†Terminate or signal all processes with given name.

Sounds Metallica-ish, doesn’t it?

  • ps –¬†Report process status
  • watch – Monitor command result

(For the watch ūüėČ )

  • nice –¬†Invoke a utility with an altered nice value

Users and systems management:

  • clear – Clear console / terminal
  • login – Log in to the system
  • passwd – Change password
  • su – Log into other user’s account
  • sudo – Run process with root rights
  • who –¬†Display who is on the system
  • whoami –¬†Display¬†what user are you currently using

Text editing:

  • cut –¬†Cut out selected fields of each line of a file
  • grep –¬†Search text for a pattern
  • head –¬†Copy the first part of files
  • more –¬†Display files on a page-by-page basis
  • vi –¬†Screen-oriented (visual) display editor

Comment time

People usually don’t know all Unix commands by heart – they collect the most useful ones in random txt file and use them (or use history command), believe me. To use Unix – you don’t have to know them all. Take it easy.

To manage your Unix account – you’ll need a login and password. Nice tool to manage Unix (or Linux) commands is Putty¬†(such fabulous UI design) – give it a try ūüôā

Command line allows you to combine multiple commands and get precise results.

On the other hand, you may be thinking how Unix will be helpful in testing activities? In the same way as in development – you are able to manage your files quickly or run shell scripts. In addition, everyone is able go through the same logs, search for useful scripts, go inside them, look around and modify, if necessary.

You can also narrow down your search results, using grep, or make sure that the log you are looking for at the moment is the same one, which has been generated
a moment ago – not last month.

Furthermore, you are able to color your results, number the rows and save all modifications. All of that  Рusing just pure commands Рwithout GUI. Sounds strange Рbut it works Рand, to be honest, a lot of people work this way.

One account – multiple users

There is a very important thing to remember when working on Unix account.

I did mention before – companies (projects) use Unix or Linux, because it is possible to work on the remote machine – even by multiple users at once. On the other hand, those users usually have rights to modify the same files. What dose it mean? You have to be precise and careful what you are doing.

I hope that my guide made you curious and you’ll experiment with Unix, Linux and command line. It is not as scary as you think.

Do you have any favorite commands? Did I miss something important?¬†Don’t be shy – you can comment down below.

Stalk me on Twitter (@KingaTest).

Cheers!

Posted in Unix, scripting

Unix and friends

UNIX

Knowing Unix command line is definitely not something that you’ll impress a nice girl, but it might be something that you’ll impress cute geek guy ūüėČ

What is Unix

As I wrote before¬†¬†Unix¬†is a family of multitasking, multi user computer operating systems. I was told that my definition was not precise enough, so for those of you, who are completely not familiar with Unix, I’ll repeat:

Unix is a OS (Operation System).

It is not a tool.

It consists of large number of tools.

It’s like a mother-ship for other OSes (such as Linux or you’re favorite Mac ūüėČ ).

In order to talk to Unix – you’ll need to be using the¬†command line.

(I hope this time it was explicit enough :)).

crVtQK

Moving on

In Unix-like systems the commands are quite often nothing else but tiny programs, which run from command line. They are form of shells. Most of the shells can be also used as scripting language, which enables the user to perform multiple tasks and write scripts to automate repetitive activities.

There is several scripting languages available (csh, ksh, bash). As far as I know, one of the most popular in Unix is bash.

Before you start a little chit chat with your console – it is good to know¬†the basics – and what I mean by that is to read about FHS –¬†Filesystem Hierarchy Standars.¬†Unless you won’t be a smart-ass.

FHS is common for Linux and Unix systems.

Examples

Having said that, let’s move on to¬†the most useful commands that allow you talk¬†to Unix.

One of the cutest commands I’ve ever seen is whoami. It is a real command. It doesn’t mean that¬†Unix would reveal the secrets of life in front of you, but after several hours of coding it is good to know your name, right?

What might be more useful in real life? What do I use on daily basis?

bash  Рswitch scripting language to bash

pwd – show current directory

cat   Рshow file content

cd    Рchange directory

/       Рhint Рin Unix (in a contrary to Windows console) we use / instead of \

cp  Рcopy

ls   Рlist

ls -l   Рlist with rights Рthis command shows hidden rights to the files inside current catalog

 -a   Рlist hidden files

ln  Рcreate hard link

rm – remove file or catalog

chmod  Рadding/removing rights to the files

-al      Рshow hidden catalogs

history – this one fellow might be useful at the beginning – it lists all previous commands

Please try it at home. Next time we’ll play a bit more with Unix. I’ll show you some tricks.

As usual, stalk me on Twitter – all of your comments are very welcome.

Cheers!

Posted in scripting, Unix

VI – Very Interesting text editor

Tenha uma

Hello Boys and Girls

As I promised, I would share all knowledge I gain, as it might by useful for any of you (either tester or not).

Last time we were talking about basic concept of Unix. It would be great to be able to talk to it, wouldn’t it? ūüôā In Unix and Unix-like systems all commands are run from command line in some script languages. There is few of them, but bash seem to be most recommended. But it is not language¬†that we will be talking about today.

Today we will be editing files.

From command line.

In Unix.

Like… really.

What is VI

There are some editors that enables you to manage files in Unix.

Editing files using the screen-oriented text editor vi is one of the best ways. This editor enables you to edit lines in context with other lines in the file.An improved version of the vi editor which is called the VIM.

Let’s start from basics.

The name “vi” is derived from the shortest unambiguous abbreviation for the ex command visual, which switches the ex line editor to visual mode. The name vi is pronounced /ňąviňźňąa…™/.

You have a file – let’s assume it is some shell script (sh) – a file that was written to run some process. You are able to see (edit) it in your Notepad ++ (I need to write about it too), but it’s not the point. You can be a smart-ass and edit it directly in Unix – using VIM. How to do it?

Open your Unix console -> Find the file location -> type vi NameOfTheFile.sh -> Click Enter.

1Tadaaaaam ūüôā

Piece of cake.

Vimtutor

For those of you, who are really interested in getting familiar with vim – there is an awesome built-in solution. It’s called vimtutor. Type¬†vimtutor in your console,¬†click Enter, and¬†you’ll have access to set of easy lessons which give you some basic abilities.

VIM is a bit like Agile for V-lovers – You have to unlearn what you have learned. Nothing there is as you assume it would be – for instance moving up and down in the console bases on 4 keys:

k

h                                                                                                                                    l

j

 

It is so straight-forward to remember that l is a shortcut for right ūüôā Right?

First thing to remember is how to get out of vim

:q!

Those 3 keys may save your life.

Hints

To be honest, vim is useful, especially, when you are working with number of files, have to edit them and basically move around. It helps. It might be colorful, if you wish and it does it’s job.

Useful hint: VI is case-sensitive. It means that you have to be careful what you are writing. :Q! won’t get you out of there ūüôā

Vim works in two modes:

  • command
  • insert

To proceed any operations – you have to be in command mode. In order to do so – press Esc twice.

To edit your file and change text inside them – you have to be in insert mode – when you want to switch to it –¬† i (insert) or¬†a (append).

Inside vi you don’t have to press Enter to confirm your operation (that is also a useful hint).

938f75ace142cfa0b22b4dbe5cc558a3

Commands and descriptions

 

What might be useful for you at the beginning of playing with insert/append mode:

  1. Editing files

i –¬†Inserts text before the current cursor location

a –¬†Inserts text after the current cursor location

A –¬†Inserts text at the end of the current line

p – Pastes a new line

 

       2. Changing files

x –¬†Deletes the character under the cursor location

X –¬†Deletes the character before¬†the cursor location

dd – Deletes the line

dd<10>РDeletes ten lines at once , starting from the place where cursor points

u – Undo of the last command

>Moves text to the right

< –¬†Moves text to the left

yy – Copies the current line

 

What might be useful for you at the beginning of playing with command mode:

:q! ‚ÄstLeaves the editor without saving

:w ‚ÄstSaves the file

ūüė° – Leaves the editor with saving

:help – Helps you

:e name – Opens file for editing

 

And couple of my favorites:

:set nu РSets the number for each line in the file (eases searching through logs a lot,                            believe me)

/              Рsearches through pattern Рfor example /Setting

:10,30s/^/#/ Рan example of how to comment lines from 10 to 30 (you can try any other                             combination on your own)

 

The vi has the capability to run commands from within the editor. To run a command, you only need to go to the command mode and type :! command.

VI runs on every Unix environment without any additional packages or libraries. It gives you the opportunity to work with your files even the the connection and editing environment are rather poor.

I hope that set of hint will be useful for you. There is plenty of online guides and examples how to play with vi. Don’t be afraid and remember – if I was able to do it – you are capable of it to.

Enjoy!