Skip to main content


Showing posts with the label Copy selected files in Linux

List completed cron jobs in Centos

Listing cron jobs or log files within a specific time frame is quite hard especially if the log or logs are quite a big file.

But of course, doing the lazy way but a smarter way is always a good option.

Use SED or stream editor.

In Centos the log is in: /var/log/cron

/var/log - path for the file
 cron - is the file that keeps the record for cron jobs, there is no filename extension

To check the logs within the 24 hours time, sed can do it easily and quickly.

Here's one line, time saver command to check the cron log file:

sed -n '/Mar 10 00:00:01/ , /Mar 11 00:01:01/p' /var/log/cron

To redirect the output to a file:

sed -n '/Apr 10 00:00:01/ , /Apr 11 00:01:01/p' /var/log/cron > cron24_hours.record.log

You can replace /var/log/cron with any other files as long as it follows the time format of Month, Day of the month and the time in HR:MM:SS format.

Download the free Linux Android App cheat sheet, see link below. It's free. Enjoy.

Cheers..till next time!


Linux bash script copy folders and files from list

In Linux special characters such as "#", "-" and other special characters cannot be access directly either by manual copy or via script. For example this path below it has the sharp "#" key:
cp "/usr/dfiles/$line/Tasks/#msgs/*.eml" "/mnt/NAS/Tasks1/line/"
Even though the path is enclosed by quotation marks, Linux system will show "No such file or directory".

Since the system is unable to find the "#msgs" directory.

As a work around in this issue is to append "--" double dash before the cp command and everything will work fine.

So, this command below will work.

cp  -- "/usr/dfiles/$line/Tasks/#msgs/*.eml" "/mnt/NAS/Tasks1/line/"
Copying one folder is quite practical to do it by hand rather by script.
But if you are copying hundreds or thousands of folders, doing it manually is quite painful.
To copy hundreds or thousands of folders to another folder the pra…

Hide the taskbar in Windows 10

Windows 10 update changes the way how to hide the taskbar.
The old way was the traditional way of hiding the taskbar just like in Windows XP, by right clicking on an empty taskbar and selecting “properties” and from the properties window there is an option to hide the taskbar automatically.
However, Windows 10 OSes that has  been updated, right clicking the empty taskbar does not show “properties” anymore.  Instead of clicking or finding properties, click on “settings” and on the settings Window, an option to hide the taskbar in desktop mode can be enabled or disabled.
See screens shot below, on how to do it:

================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet:
Multiplication Table for early learners
Catholic Rosary Guide  for Android:…

Linux protect file from deletion

To protect file from deletion in Linux system, the chattr command is able to set the attributes that protects the file.

chattr +a my_protected_file.txt

chattr +a  == means that the file can be appended and the file can't be deleted as well.

To set the file to immutable, "+i"  attribute can be used.

chattr +i the_protected_file.txt

Immutable file is protected from deletion and the original contents of the file is also preserved because no changes can be made.

This command below:

ls xx*.txt >> the_protected_file.txt

The command above will show "permission denied" if the file is immutable.

If the file is set with +a, then the above command will append the output of "ls" to the file.

To unset or removed the attribute use the minus sign, "-a", "-i".

For example, chattr -a the_protected_file.txt or  chattr -i the_protected_file.txt

To know more about chattr type the command below:

man chattr
chattr --help
info chattr

This wiki link shows …

Linux search string in text files

Search a string or a pattern in text files without opening the file.

Grep is a handy tool to find or search a string in text files.

Grep is an available tool in Linux and Unix OSes.

So if you have some data or information stored in text files and forget where the file is located.

As long as you know the keyword or a string to search for, then grep and find command will be your utmost friend.

"Grep" and "find" are tools to make life easier to get the information you want but how to use it?

Command below will search recursively in the patch specified for all the text files and display the file where the match is found, output will also include the path and the filename.

find /home/00_Notes -name '*.txt' -print0 | xargs -0r grep -H 'vanity baseline'

The xargs -0r,  is zero r.

The above command will search recursively in all folders and subfolders for text files which contains the string "vanity baseline".

If a match is found an output will be…

Linux Awk insert text at specific line number

One liner command for Awk to insert a new string or text at a specific line number on a file.

awk 'NR==3{print "new line text at row 3"}1' original_file.txt > modified_file.txt

'NR==3 --insert string or data at line number 3
"}1' -- number 1 (can be any number) means to append the new text or string
> modified_file.txt -- redirect the output to a new file

To find out more, type this at the terminal.

                info awk
                info print
                man awk
                man print

Or you can redirect the output of info and man to a file for offline viewing.

Check out links below for other examples of awk command.

Tools for Linux Troubleshooting

A basic troubleshooting skill is just necessary for a System Administrator. One way or another thing will go south, no matter how you make sure that the system is working perfectly healthy.
Patches, updates, new security holes or vulnerabilities will definitely change the whole system and if something goes wrong, then a working system will need troubleshooting.
Even new system configuration changes that are not recorded and left forgotten and cause an adverse effect that is applied to the system will cause chaos and more time will be spent in troubleshooting, only to find out that a minor change causes the whole thing.
Here are some tools below, which could provide a basic troubleshooting to a Linux system.
A general purpose logs files, which shows messages from the system.
    tail -f /var/log/messages
/var/log/  -- directories which contains log files for the system. This directory can easily fill up the space of the whole system if not the log files are not managed properly.

Linux Check IP Address or MAC Address

Linux good old command to check IP Address is  ifconfig which works probably in all distro.

There is already new implementation on how to check the IP Address via command line from some distro.

Some distro now supports the command, ip addr show which also displays the IP Address.

The ifconfig command has a long output, which is quite scary if you are new to Linux.

To filter the desired output in ifconfig, awk or gawk will come to the rescue.

Below are some examples on how to do it:

Display only the IP Address:

ifconfig -a | awk 'NR==2'

Sample Output:
inet addr: Bcast: Mask:

Display only the MAC Address:

ifconfig -a | awk 'NR==1'

Sample Output:
eth0  Link encap:Ethernet  HWaddr 07:00:16:f1:bb:ac
HWaddr is the MAC Address

Display both IP and MAC Address:

ifconfig -a | awk 'NR==1,NR==2'

Using awk command ifconfig output is displayed line by line.

Some other useful command below to check users login in Linux.

last /var/log/wtmp
last /v…

Linux count string occurrences

How to use grep in Linux to count string occurrences?

grep -oi "error" /home/xlog/xerrorlogs.txt| wc -l
Sample output: 5 (if there are 5 matches)
Grep will look for the word "error" in xerrorlogs.txt and count its occurrence per line.
Grep will find the string occurrence without case sensitivity.
Grep parameters: -o --only-matching Print only the matched (non-empty) parts of matching lines, with each such part on a separate output line. -i --ignore-case Ignore case distinctions, so that characters that differ only in case match each other. wc - print newline, word, and byte counts for each file -l, --lines print the newline counts
If "wc -l" is omitted: grep -oi "error" /home/xlog/xerrorlogs.txt
Grep will display the match string.
Sample output: Error Error Error Error Error
If Grep uses -c parameter: grep -oic "error" /home/xlog/xerrorlogs.txt 
Sample output: 4
The output is "4" if there are 5 matches since counting starts at ze…