Search Results for: cat

mtime – file modification timestamp in Unix

mtime is one of the three timestamps in Unix that are maintained for each file in most of the filesystems.

Purpose of mtime

The real purpose of the mtime timestamp is to track the last time of changing the contents of a file. Various commands will allow you to access this information later. For example, ls command allows showing list of files along with their last modification times (it's also possible to get ls to confirm the last access time (atime timestamp)for any file).

mtime example

Here's how you can see mtime in real life. Let's create a file named example.txt and get a full ls listing on it:

greys@ubuntu:~$ date
Fri Sep 28 10:25:40 IST 2012
greys@ubuntu:~$ > example.txt
greys@ubuntu:~$ ls -l example.txt
-rw-r--r-- 1 greys greys 0 2012-09-28 10:25 example.txt

As you can see, the last modification of the "example.txt" file is 10:25am.

Now let's wait a minute:

greys@ubuntu:~$ sleep 60

…confirm the file's mtime is still the same:

greys@ubuntu:~$ ls -l example.txt
-rw-r--r-- 1 greys greys 0 2012-09-28 10:25 example.txt

… and now make the change by adding a line "change" to our file:

greys@ubuntu:~$ echo "change" >> example.txt

And if we check the file's mtime timestamp, it will be updated – in my case 10:27am:

greys@ubuntu:~$ ls -l example.txt
-rw-r--r-- 1 greys greys 7 2012-09-28 10:27 example.txt

More info on mtime

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

cat – concatenate files and print to the standard output

cat is a simple yet very useful Unix command. It takes a name of one or more text files, and then shows their contents to the standard output as one stream of data.

cat command example

greys@ubuntu:~$ cat /etc/kernel-img.conf
do_symlinks = yes
relative_links = yes
do_bootloader = no
do_bootfloppy = no
do_initrd = yes
link_in_boot = no
postinst_hook = /sbin/update-grub
postrm_hook   = /sbin/update-grub

for two files, it looks like this:

greys@ubuntu:~$ cat /etc/issue
Ubuntu 7.04 \n \l
\
greys@ubuntu:~$ cat /etc/issue /etc/kernel-img.conf
Ubuntu 7.04 \n \l
\
do_symlinks = yes
relative_links = yes
do_bootloader = no
do_bootfloppy = no
do_initrd = yes
link_in_boot = no
postinst_hook = /sbin/update-grub
postrm_hook   = /sbin/update-grub

[Read more...]

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

locate – quickly find files in Linux

Today I'd like to show you one more option you have when searching for files in Linux. If you have a locate tool installed, you'll be able to find any file almost instantly.

How does locate command work?

locate uses a pretty simple principle – instead of going through your filesystem directory tree every time you need a certain file found, it consults a database which stores locations of most files in your system. The locate database (locatedb) is updated nightly with a separate command. The update occurs during night hours when peak usage of your system is very unlikely, but this means that using such a database through the day will provide instant results.

[Read more...]

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

How To Find a Location of a Directory in Unix

Very quick tip for you today, I just see that many of visitors of this block are curious how they can find a directory in Unix – and so here's a command to help you do just that.

Finding directories in Unix

There's nothing better than to employ the find command. As you might remember, among many things, this wonderful tool allows you to search files by their type. Since nearly everything in Unix is a file, this means you can find directories.

[Read more...]

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

tee: Replicate Standard Output

Now and then I come across a situation when I need to run a script or a Unix command and would like to not only see the output of it on the screen, but also save this output to some log file. Redirecting the standard output using standard Unix stream redirection isn't always useful because your output will either be shown to you, or sent to the file – but not both at the same time

tee command

That's where the tee command becomes really useful. You pipe your output to this command, and let it take care of the rest.

[Read more...]

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

7zip ubuntu

7zip is a versatile open source file archiver that uses the high compression 7z archive format, but also supports many others including ZIP, GZIP, BZIP2, TAR, WIM, XZ for both packing and unpacking, and even more for just unpacking such as RAR, ARJ, CAB, DEB, DMG, MSI, RPM and others. Check out the 7zip web site for more information.

Install 7Zip

7Zip is available for Ubuntu, and Linux in general in form of p7zip, which is a cross-platform version of the program. It is readily available for install from Ubuntu repositories using the Ubuntu Software Center or the APT tool. You can also install 7zip-rar to include RAR support in 7Zip.

Therefore, to install 7zip in Ubuntu simply search for it in the Ubuntu Software Center and install from there. If you want 7zip-rar tick the "Non-free rar module for p7zip (p7zip-rar)" checkbox from Optional add-ons.

To install both packages quickly from the command line just open the Terminal and run the following APT command:

sudo apt-get install p7zip-full p7zip-rar

That's all there is to it! Now you can use 7zip to package and unpackage files and folders using both command line tools 7z and 7za, and graphical tools like Ark, File Roller, and even the Nautilus File Manager.

Using 7Zip

To compress an archive of files and folders with 7Zip into a .7z compressed archive run the following command:

7z a pictures.7z Pictures/

In this example we compressed the Pictures folder into a pictures.7z archive. The "a" function stands for "add" or "archive" for adding specified files to the archive. We can also specify an absolute path to files and folders we want to compress by replacing Pictures/ above with, for example, /mnt/data/Pictures/.

We can also use 7z to compress folders into an archive of a different type, such as ZIP, by passing a -t switch followed immediately by the archive type. To create a pictures.zip we would then run the following:

7z a -tzip pictures.zip Pictures/

To unpack the archive to a specified location we would use the "e" function, which stands for "extract", like this:

7z e pictures.7z

This would extract files and folders to the current directory we are in.

Of course, you don't need to use the command line to compress and extract files with 7zip. Having it installed in Ubuntu automatically integrates it with Nautilus, the Ubuntu's File Manager, which allows you to seamlessly extract 7z and other archives by just right clicking on it and choosing "Extract here".

To compress files and folders just select them, right click, and choose "Compress…". A dialog will open that allows you to select 7z as the format, among many others.

Besides Nautilus having 7Zip installed also adds 7Zip support to other archiving tools such as File Roller and ARK so they can be used as normal to handle 7z archives, and others supported by it.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Ubuntu rar

RAR is a popular proprietary file format and software for compressing and archiving files, known for its ability to span multiple RAR files into a single archive, which is useful for compressing, archiving, and transferring large files.

Linux, including Ubuntu, has had RAR support for some time, but due to RAR's proprietary licensing and restrictions support comes through multiple programs. Luckily all of them are easy to install, and if you don't particularly care for the license you might just want to install them all in one go, especially unrar-nonfree and rar, and enjoy complete support for all RAR files, and all operations it allows you to do.

The following are the relevant programs and what they support, by their package names:

  • unrar-free – opens some .rar files, does not support RAR v3.
  • unrar-nonfree – opens all RAR files.
  • unar – an alternative option with full RAR support
  • 7zip – supports opening and extracting rar files
  • rar – a program for both creating and extracting rar archives

That about sums up what we've got, in addition to graphical archiving applications that use the support of some of these programs to handle rar files, among others.

Now let's try to keep it simple. If you just want the ability to open and extract all rar files, and also easily create rar archives then install unrar-nonfree and rar using the Ubuntu Software Center or with these simple apt-get command:

sudo apt-get install unrar-nonfree rar

With this stuff behind the scenes your Ubuntu can now do anything you want with rar files, right from the File Manager itself. Just right-click on any .rar file and choose "Extract.." and you're done. Or if you want to compress some files just select them, right click, and choose "Compress…". Then pick .rar from the list of formats and click "Create". You can then also use File Roller, Ark, and similar programs with rar files as well.

If you prefer to decompress with the command line here are a few basic examples to get you started. To extract an existing .rar archive run the rar command with the "e" function, which stands for "extract":

rar e pictures.rar

You can also use the unrar command the same way:

unrar e pictures.rar

To create a new archive run rar with the "a" function, standing for "add" or "archive", like this:

rar a pictures.rar Pictures/

You can also add multiple files into the archive:

rar a pictures.rar picture1.jpg picture2.jpg picture3.jpg

And just to make things slightly more interesting here's how to create multipart rar archives, those that take the original big file and compress it into multiple .part01.rar, .part02.rar etc. files.

rar a -v50M movie.rar movie.mp4

The "-v50M" tells rar that each rar part will be 50 megabytes in size. You can put any size here. For example if you want 5 MB for each part you would type "-v5M" instead. Extracting these files works the same way as extracting any other archive except you have to extract the first part, the .part01.rar, like this:

rar e movie.part01.rar

It will then take data from all the parts and put it together into the original file.

For more information you can consult the manual pages for rar by running man rar. Don't mind it saying "this is a trial version". It's the same kind of perpetual trial as WinRAR on Windows.

And that's how you roll with rar on Ubuntu.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

GRUB Boot Loader

GRUB is the "GRand Unified Bootloader", a boot loader that supports multiple boot options, such as various Linux boot modes, and other operating systems like Windows, BSD, and so on.

A boot loader simply loads up what's necessary for an operating system to start up, such as where's the kernel and with what options should it be loaded. Once it loads it up it hands off control to the kernel to complete the process.

You can configure GRUB boot options by editing the /boot/grub/grub.cfg or in some systems /etc/grub.conf file or using one of the graphical tools that make doing this easier and safer. For most Linux users the grub configuration will be automatically created, even including other operating systems you might have installed.

Key part of the grub.cfg file is where it lists the operating system options. One such entry may look something like this (from a sample configuration):

menuentry 'Ubuntu, with Linux 2.6.32-24-generic' –class ubuntu –class gnu-linux –class gnu –class os { recordfail insmod ext2 set root='(hd1,6)' search –no-floppy –fs-uuid –set 6655ee5e-45d1-4d1c-9a7d-10f30f16e745 linux /boot/vmlinuz-2.6.32-24-generic root=UUID=6655ee5e-45d1-4d1c-9a7d-10f30f16e745 ro quiet splash initrd /boot/initrd.img-2.6.32-24-generic }

The menuentry line defines the name of the entry and proceeds to specify some options. Key options here are set root, which specifies the partition on which the root directory of the operating system resides, the linux option followed by a path on the specified root directory where the kernel is and with which options to load it, and the initrd line specifying the location of the initialization file.

Besides these menu entries and options you can also set some general options for GRUB. A couple basic ones that may be of interest are GRUB_DEFAULT and GRUB_TIMEOUT.

GRUB_DEFAULT sets which of the menu entries will be selected by default, and booted into automatically after a timeout. Its numeric value corresponds to the order in which a menu entry is listed, keeping in mind that it starts at "0", not "1". So if you want the first menu entry to be the default you set GRUB_DEFAULT=0 or if you want the third one to be the default set GRUB_DEFAULT=2, and so on.

You can also set GRUB_DEFAULT=saved if you want the default to be the last selected entry on the previous boot.

GRUB_TIMEOUT simply sets the number of seconds it will count to before booting the default entry automatically. It's typically set to 10 seconds: GRUB_TIMEOUT=10. You can change it to whatever you want, or put "-1" if you want to disable the timeout and have it boot only when you explicitly tell it to. Of course, "0" will make it boot immediately.

If you've edited the grub.cfg file and saved it you also need to update grub with the new configuration by running this command:

sudo update-grub

Finally, if you need an easy way to modify GRUB bootloader configuration with a graphical user interface you can use grub-customizer or Boot Repair.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Hard Link

Files in a file system are kept track of by so called "index nodes" or "inodes" which store some metadata about each file such as its type, file, access/change/modification/deletion times, owner, group, and so on (not including file names though). Typically, only one file can be associated with a single inode, but this is where hard links come in.

A hard link allows multiple files to be associated with a single inode where one file is a link, and the other is the original file. The key thing is that the link refers to the same inode pointing to the same data. This is different from a soft link, also known as a "symbolic link" or "symlink" which only contains an abstract path to the original file rather than being associated with the original file's inode.

Because of this hard links don't work across multiple file systems or partitions, and they also cannot link to directories. Their biggest advantage, however, is that they stay linked to the original file wherever on the same file system they are, even if the path location of the original file changes (because the link refers to the inode not the path).

In contrast soft links refer to nothing if the original file changes its location, becoming broken links. Their advantage, however, is that they can link across file systems as well as link to directories (because they refer to the path).

To create a hard link to a file use a command like this:

ln /home/user/file.txt /home/user/file-link.txt

The /home/user/file-link will be a hard link to /home/user/file, and will refer to said file even if you move it to /home/user2/file.txt.

To create a soft link or a symlink run the same command, but with an -s option:

ln -s /home/user/file.txt /home/user/file-link.txt  

That will create file-link.txt as a link to file.txt, but if you move file.txt from /home/user to /home/user2 the link, still pointing to the original path, will be broken.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

How to Use visudo

The visudo command is a safe and secure way of editing the /etc/sudoers file on UNIX and Linux systems. Since the sudoers file determines which users can run administrative tasks, those requiring superuser privileges, it is a good idea to take some precautions when editing it, and that's what visudo does.

It locks the sudoers file so it cannot be edited by anyone else simultaneously. It also checks the syntax of your edits and provides basic sanity checks. If someone else is editing the file you'll get a message to try later, and if there are errors in your edits it wont save them.

Preventing simultaneous editing by someone else is helpful to ensure your edits aren't lost, and saving a sudoers file without errors is important because you could otherwise end up locked out of your system. An unreadable sudoers file will prevent you from running administrative tasks by using the sudo command or becoming root, and editing the sudoers file itself requires those privileges. So you really don't want to screw that one up.

Visudo is basically a wrapper for a text editor such as vi or nano. Vi is traditionally the default unless your distribution or OS has something else set up. For basics on how to use vi for editing check out the vi survival guide.

Visudo has a built in list of supported editors that can be used, and you can change which it will use by setting the "EDITOR" environment variable on the command line like this: export EDITOR=nano. This will set nano as the default editor. To save this permanently add the same line to the .bashrc file in your home directory. On Ubuntu, where nano is actually set as the default, you can also change it by running sudo update-alternatives –config editor and then selecting your preference.

Editing Sudoers

To open up the /etc/sudoers file for editing with visudo simply run sudo visudo.

Before making any edits it's a good idea to check the existing configuration, and understand what everything means. One line you'll definitely encounter is this:

root    ALL=(ALL:ALL) ALL

This gives the root user all of the superuser privileges, as can be expected. The format of the rule set such as this is as follows:

user hosts=(users:groups) commands

What you're doing is specifying which commands can a given user run under which circumstances. In case where all of them are set to ALL, like for root, it means that the user can run all commands on all hosts, as all users and groups.

If all you want is enable another user with the same powers as root, obtainable by issuing the sudo command before the desired command, you can just copy the root line and change "root" with your username, in this example "daniel":

daniel    ALL=(ALL:ALL) ALL

But if you don't want to give all of the privileges you can adjust the rules. For example you can allow "daniel" to only run certain commands:

daniel ALL=(ALL:ALL) mytop,cat,tail

Besides users you can also give superuser permissions to groups using a % indicator:

%admin ALL=(ALL) ALL

This would allow all users in the admin group to run all commands as root.

Aliases

Finally, you can set up aliases to group multiple entries into a single one for use in these statements. There are four types of aliases: User_Alias for listing users, Runas_Alias for listing users a given user can run as, Host_Alias for listing hosts, and Cmnd_Alias for listing commands.

Aliases are useful if you have a more complex set up with multitude of users that should have varying degrees of privileges on the system. To set up an alias just state the alias type, its name, and then the list of users, hosts or commands you want to associate it with. For example to set up a User_Alias you can do this:

User_Alias MANAGERS = steve,bill,james

All the other aliases follow the same format only with the different specified type, and listing different types of things, like users, hosts or commands. If we wanted to put the three commands from the above example with the "daniel" user under an alias we could do this:

Cmnd_Alias READ = mytop,cat,tail

And then instead of listing these two commands in our configuration for daniel we can just specify the READ alias:

daniel ALL=(ALL:ALL) READ

It works the same way for other types of aliases. If we want to give the same privileges to users steve, bill, and james we can say:

MANAGERS ALL=(ALL:ALL) READ

You get the idea.

These are the basics of using visudo and editing the sudoers file with it. We recommend you check out the manual pages if you ever need more detailed reference, like man visudo and man sudoers. You can also see a sample sudoers file with many examples at its web site.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS