Skip to main content

Posts

Showing posts with the label Linux basic device utility tools

Linux find accessed and modified files

Finding accessed and modified files might be necessary at times to check or for audit purposes.
If files kept in a folder or directory has been accessed or modified but should not be the case then something dubious is going on. 
In Linux finding accessed and modified files can be done in a one liner command.
find /home -type f -amin -60 || -mmin -60 -print
Above command will find or show any files accessed within the last 60 minutes with the option "-amin" and it will show also the files modified within the last 60 minutes with the option "-mmin".
A shell script can be created and further processing can be done when files are detected.
The time can be adjusted if there's a need, but a more robust solution to check any accessed or modified files should be a file system watcher, but above command is quite helpful to check any activity that should not be occurring.

Cheers..till next time!================================Free Android Apps:
Click  links below to find out mor…

Linux sed remove duplicates and get unique values

Removing duplicates and getting unique values is quite easy provided that the input data follows a specific format, for example the string or raw data has spaces in between.
But a dilemma can occur if the data has no spaces in between the characters of the string, instead of spaces it is separated by dashes.
So, how to remove duplicates, get the unique values and still retain the format of the raw data?
Like this raw data: (just a sample string) the-quick-brown-fox-jumps-over-the-lazy-dog-jumps-over-the-lazy-cat-jumps-over-the-rabbit
When removing duplicates and getting unique values via this command:
sort duplicates.txt | uniq (this will work if the data is separated by spaces)
duplicates.txt assumes that it has the string as illustrated above.
Sample output:

The output will be the exactly be the same with the input. Why? It is because the whole string is treated as literal one string, because the dashes connect between the character eliminating the space delimiter.
Example, if the requiremen…

How to copy a line using Nano editor?

Copying in Nano needs a few keys to copy and paste. 
Copy and paste goes hand in hand, there’s no point to copy if you cannot paste.
So, how to copy using Nano editor in Linux?
To copy in Nano editor , a few key strokes are needed.
On the beginning of the line that will be copied press:
ctrl + 6 (to set a mark)
Press “end” key or use right arrow to go to the end of the line.
If multiple lines are to be copied used down arrow to select the lines.
After selecting the line or lines to be copied, press:
alt + 6 (to set unmark and all line/s selected is copied to the clipboard already.
To copy the line or lines, position the cursor where the line/s will be copied and press:
ctrl + u (this will paste the line/s or whatever is on the clipboard)

That’s it, quite a few key strokes to remember but if you’re in a command line or in Putty; then these key combination to copy and paste in Nano is really a life saver.

Cheers..till next time. :)


================================ Free Android Apps:
Click  link…

Replace column value of text files using awk

AWK is a handy tool to process large text files.
Ever came across a need to replace 100 or more text files value in a single column?
Of course, the old school method is to do it manually which is definitely time consuming and not efficient at all.
Below is a sample code how to replace value in a column for all text files found in the directory specified.
#!/bin/bash
for file in /home/pc_text/Documents/source/*
do xfilename=$(basename $file)
#echo "$xfilename" if you need to display the filename while the code is running
awk 'FNR==1{print $0}' $file >> /home/pc_text/Documents/output/$xfilename
awk 'NR>1{print $1 , $2 , $3 , $4, "0.873" , $6 , $7 }' $file >> /home/pc_text/Documents/output/$xfilename
done
Code explanation:
for file in /home/pc_text/Documents/source/* - get all text files for processing
xfilename=$(basename $file) – get all the filename of the text files
awk 'FNR==1{print $0}' $file >> /home/pc_text/Documents/outpu…

Add a remote folder in Linux

Adding a remote folder in Linux or basically mounting a folder in Linux system is quite easy.
Like in copying and moving files or folder, it requires a source and a destination.
A simple copy command in Linux is:
cp --source or any file to be copied--  --destination or where to copy the file--

In mounting a folder, using mount command does require also a source and a destination. man mount will display the available options for this command.
Since source and destination is a pre-requisite in cp, mv ormount command.
In mounting a remote folder, the destination folder must be ready or created first before typing the mount command.

Linux has default destination for mounting folder which is /mnt folder.
Mounting a remote folder can be also done on other location such /home /usr or other preferred locations.

To mount a folder in /mnt directory, a sub folder must be created first.
To create a folder, type: mkdir /mnt/remote_folder_101

Once the sub folder or the destination folder is created…

Linux folder with special characters

Operating system or even applications programs does restrict some characters since it's either used by the system or it is not permitted for some other reasons.

In Linux special characters like ampersand, dashes and other special characters should not be used as folder names or even file names.

But some users or even IT folks violate this rule without knowing the consequences it holds.

Of course, whatever characters available on the keyboard should be used or else what's the point of having them on the keyboard. :)

But there are consequences in using those mentioned characters.

In Linux terminal, if the folder name has an ampersand on its folder name like the image below it cannot be copied easily.

For example navigating in Linux through the terminal using "cd" should be smooth but sometimes simple things can be tough if you don't know how to get around with it.


Navigating folders with special characters using "cd " command is quite simple by enclosing…

Linux copy files and change date or time

So, you want to copy files from one folder to another folder and change both the source and destination timestamp?
Linux can easily change or modify the timestamp with ‘touch’ command.
A one liner command with the help of ‘pipe’ to pass the arguments and ‘xargs’ to execute multiple commands in a single line can easily accomplish this task.
Command below will copy the files in the current directory where ‘ls’ command is executed to the directory ‘xfiles’, timestamp will be changed to 7 hours less from the current time.
ls | xargs -I % sh -c 'touch -d "7 hours ago" %; cp -p % ./xfiles';
-p option is important to preserve the modified timestamp
Note: the above command will change the timestamp for both source files and destination files.
Command below will change the source timestamp, but the destination timestamp will be the date and time that the command was executed.
ls | xargs -I % sh -c 'touch -d "2 Aug" %; cp % ./x';

Notice, that the -p option is omitted …

Linux list folders recursively

To list all folders on the root directory, type:
  ls -d /*
Sample Output:
/bin     /dev   /smsbackup  /lost+found  /mnt  /proc  /selinux  /tmp /boot    /etc   /lib        /media       /net  /root  /srv      /usr /cgroup  /home  /lib64      /misc        /opt  /sbin  /sys      /var
To list all folders on the current folder, one level depth only; type:
[xxx@mx var]$ ls -d */
Sample Output:
account/  cvs/    games/  local/  lost+found/  opt/       spool/  yp/ cache/    db/     gdm/    lock/  sms/        preserve/  tmp/ crash/    empty/  lib/    log/    nis/         run/       www/

List all folders and sub folders, 2 level depth or 2 level hierarchy; type:
[xxx@mx var]$ ls -d */*/
Sample Output:
cache/abrt-di/        lib/misc/        log/ConsoleKit/      run/portreserve/ cache/cups/           lib/mlocate/     log/cups/            run/rhsm/ cache/fontconfig/     lib/net-snmp/    log/sms/            run/saslauthd/ cache/foomatic/       lib/nfs/         log/gdm/             run/sepermit/ cache…

Use tar command to backup in Linux

tar or tape archive is a basic tool that every Linux user should know.

Its syntax is quite different from the traditional copy or move command.

Since tar works from a 'current working directory' perspective.

Traditional syntax for copy or move is source then the destination.

In tar, the syntax is the filename or the path where the backup will be save and the source or the folder to be backed up or archived.

Typing: man tar (at the terminal shows the following)

tar -cf archive.tar foo bar
# Create archive.tar from files foo and bar.

Basically, it's like the destination where the file will be saved and the source or the files that will be archived.

Here's a simple bash script that will append the time and date the tar file was executed to the filename of the tar file.

If the bash script is automated via cron, the filename of the tar file will include the date and time the cron executes the script.


#script begins

#!/bin/bash
NOW=$(date +%Y%m%d%H%M)
echo $NOW
tar -Pczvf   /mnt/o…

Bash check if folder exists

Simple bash script for beginners trying to get their hands wet on bash scripting.
The code below will ask for input and the input should be a string or a folder name.
The script will just do a basic ls and if the folder name is found it will show “The folder is found” or else the next echo statement is displayed.
Instead of simply displaying an echo command a function or another script can be called to execute a process further.
#!/bin/bash # This script will ask for a string or folder name and check whether it is present or not
echo "Enter folder name, followed by [ENTER]:"
read folder_name
ls | grep ^$folder_name && echo "The folder is found" || echo "The folder cannot be found" #Script ends
It’s a one liner evaluation, some sort an if then else in other programming languages, the && statement is executed if the evaluation is true, and if the evaluation is false the || is executed.
The read folder_name line, is the statement that will hold the inp…