Skip to main content

Posts

Showing posts from 2017

A command to list all users? And how to add, delete, modify users?

I need a command to list all users in terminal. And how to add, delete, modify users from terminal. Solution:   To list all local users you can use: cut -d: -f1 /etc/passwd To list all users capable of authenticating (in some way), including non-local, see this reply: https://askubuntu.com/a/414561/571941 Some more useful user-management commands (also limited to local users): To add a new user you can use: sudo adduser new_username or: sudo useradd new_username See also: What is the difference between adduser and useradd? To remove/delete a user, first you can use: sudo userdel username Then you may want to delete the home directory for the deleted user account : sudo rm -r /home/username (Please use with caution the above command!) To modify the username of a user: usermod -l new_username old_username To change the password for a user: sudo passwd username To change the shell for a user: sudo chsh username To change the details for a user (for example real name): sudo chfn u...

Practical examples of Touch command on Linux

Linux Touch command The touch command can be used to modify the access/modification timestamps of files. It is more often used to actually just create an empty file quickly. This post shows some very simple and quick examples of using the touch command to modify timestamps or create files. 1. Create a blank file To simply create a blank file with touch command, use the syntax below. $ touch abc.txt If the file already exists, its access time will be updated. 2. Create multiple files with touch To create multiple files, specify their names together separated by a space. $ touch abc.txt cde.txt xyz.txt 3. Create lots and lots of files If for some reason you wish to create lots of files, then commands like these would be very helpful # Create files with names A to Z $ touch {A..Z} # Create files with names 1 to 20 $ touch {1..20} # Create files with extension $ touch {1..1000}.txt # Create 10K files $ touch {1..10}{1..1000} And then use the ls command to see what all has been created. 4....

Find filenames list by content search and file format

Generally computer users can memorize the locations of different files that they read or store. As the number of files increases, they can use descriptive filenames making it easy to guess the content by looking at the file name. However when the number of files grows higher & higher in number, it becomes a pain to remember locations or filenames. Also when a computer is used by some other person than the owner, it becomes even harder to locate some files. Search by partial name find -name "<filename pattern>" Above command is useful only if you can remember the filenames at least some parts of the file name. If you are a software developer, you know how many times you would want to search files based on the content. Search by content find <path> -name "<file name pattern>" -exec grep -l "<text to search>" {} \; Above command can be used to find the files based on content search. For example to find a file with a value say ...

grep Word Count Command

How do I count words using grep command under Linux / Unix like operating systems? You can pass the -c option to grep command to suppress normal output and display a count of matching lines for each input file. The syntax is follows: grep -c "string" file In this example, search for a word called ‘var’ and display a count of matching lines: grep -c 'var' /etc/passwd You can pass the -v option to count non-matching lines: grep -v 'var' /etc/passwd However, this will not count words. To count exact matched words, enter: grep -o -w 'word' /path/to/file/ | wc -w The grep -o command will only display matched words and the wc -c command will display the word counts.

A Brief History of the UNIX and Linux Operating Systems

Understanding the origin and evolution of a particular technology often helps when learning how to use it. Like many new technologies, the development of these two operating systems did not start as a conscious effort to develop a commercial product. This makes their current level of popularity and utilization even more remarkable. - The UNIX Operating System - The Linux Operating System Operating System Concepts Most operating systems have certain traits that differentiate them from each other. The UNIX and Linux operating systems are no exceptions to this fact. Having a general understanding of a few of the design traits for these operating systems will assist you in using them more effectively. - The UNIX and Linux operating system environment - The File system - The superuser (root) account Getting Connected, Logging in, and Logging out Before using a computer system, you need to connect and in most cases be authenticated as a valid system user. Methods for connecting and being au...

Grep Command Examples of How to Search a File for a Pattern

The UNIX Grep command searches files for a user-specified text pattern. It returns a list of the matching words or shows each line of text that contains them. You can broaden the results by using wildcards. Grep also has the ability to count instances of a search phrase that appear in a file. The syntax for this UNIX command includes three parameters, but only the text pattern is required: grep [options] pattern [filename] If it contains multiple words or special characters, be sure to place single quotation marks before and after the pattern. The UNIX shell does not interpret dollar signs, brackets, parentheses, pipes or asterisks in the same way as letters. For example, an asterisk is treated as a wildcard when you don't use quotes. If you leave out the filename, UNIX will collect data from the standard input device. This is usually the keyboard. (1) Basic search with no options The following example shows what happens if you use grep to search for the word "unix...

Linux find file names with given string

  I'd like to find all files in the current directory and subdirectories whose name contains the string "ram". Solutions: 1.  find . -type f -name "*ram*" The find command will take long time because it scans real files in file system. The quickest way is using locate command, which will give result immediately: 2. locate "ram" If the command is not found, you need to install mlocate package and run updatedb command first to prepare the search database for the first time. How to install locate command on Amazon Linux? $ sudo yum install mlocate $ sudo updatedb $ sudo updatedb $ locate "ram" /home/work/ram.txt /usr/share/mime/application/vnd.google-earth.kml+xml.xml /usr/share/mime/application/vnd.google-earth.kmz.xml /usr/share/mime/text/x-google-video-pointer.xml 3. find . -type f -exec grep -H 'text-to-find-here' {} \; Usage : find . -type f -exec grep -H 'ram' {} \; 4.grep -rnw '/path/to/somewhere/...

Find Command that returns line number of the string

I have a bunch of files organized into directories..All these are text files (c/c++). I am trying to understand this code and i need to look at the declarations of many variables..How can i use find command to get the exact location( File Name with line number(s) ) using Find command in ubuntu linux?? Or is there any graphical tool for doing the same? Answer: You can do this with grep.   grep -n 'search-term' *.c   will give you the filename and line number where the term appears. This will fail if there are sub directories     You can use grep -nr 'search-term' *.c -H, --with-filename     

Files creation

In LINUX files can be created by using any of the three methods given below:        1.  Cat command (cat - CONCATENATION)        2. Touch command        3. Vi editor 1. cat command: cat stands for CONCATENATE. This is the basic command when we start learning LINUX/UNIX, as the name suggest it is used to create new file, concatenate files and display the content of the files on the standard output.     Different examples of cat command which will be useful for the beginners. 1. Creating new file 2. View the contents of the file 3. View the contents of  multiple files 4. Display the output of a file using page wise 5. cat command without filename arguments 6. Display the content of a file with line numbers 7. Copy the contents of a one file to another file 8. Appending the contents of one file to another file 9. Redirecting the output of multiple files into a single fi...