Filter, find, and user and group management commands for Linux files and folders

Source: Internet
Author: User
Tags echo command sorts stdin

1. List the user name of all logged-on users on the current system, note: The same user logged in multiple times, only one time can be displayed.

The commands for viewing system users are:
(1) Getent passwd

View all user information on all systems

(2) W

View the process information that the logged-on user is using, and the information used by the command originates from the/var/run/utmp file

The information for the W command output includes:
-User Name
-User's machine name or TTY number
-Remote host Address
-time when the user logged on to the system
-Idle time (small function)
-time spent in a process attached to a TTY (terminal) (Jcpu time)
-time spent in the current process (pcpu time)
-commands that the user is currently using

The W command can also use the following option parameters:
-H Ignores header file information
-U Displays the load time of the result
-S does not show JCPU, PCPU, logon Hours

(3) Who

View (log in) the user name and the process that was started, the output is: User name, TTY number, time date, host address.

View user's user name with who value: Who | Cut-d '-f1

The users command can be used to output the user name of the login

(4) WhoAmI

Check the name of the login you are using

2. Remove the information about the user who last logged in to the current system.
Command last information about the user who finally logged on to the current system
Last


Parameters
-a the host name or IP address from which to log in to the system, displayed on the last line.
-D converts the IP address to the host name.
-F < record file > specify record file.
-N < Show columns > or-< Show columns > set the number of displayed columns for the list.
-R does not display the host name or IP address of the login system.
-X displays information such as system shutdown, reboot, and execution level changes.

View information about the last user logged on to the current system:
Last-1 or Last |head-1

3. Remove the shell that is the user's default shell on the current system.

Command: cut-d:-f7/etc/passwd |uniq-c |sort-n |tail-1

Uniq usage
Parameters
-C: Displays the number of occurrences of the row next to each column.
-D: Displays only the rows that appear repeatedly.
-F: Ignores the column specified by comparison.
-S: Ignores the comparison of the specified characters.
-U: Show only one row at a time.
-W: Specifies the character to compare.
-N: The first n fields are ignored along with the blanks in front of each field
+n: first n characters are ignored

Sort usage
Parameters
-B ignores whitespace characters that begin before each line.
-C checks whether the files are sorted in order.
-f sort, ignores uppercase and lowercase letters.
-M sorts the first 3 letters according to the abbreviation of the month.
-N Sorts by the size of the numbers.
-o< the output file > The sorted result into the specified file.
-R is sorted in reverse order.
-t< Delimited character > specifies the field separator character to use when sorting.
-K selects which interval to sort.

Tail usage
Parameters
Default to the end of the file, the last 10 lines are shown by default
The-n parameter can set the number of rows displayed,
The-f parameter is displayed automatically when the content of the file increases.

4, the third field in the/etc/passwd value of the largest 10 users of the information is changed to uppercase and then saved to the/tmp/maxusers.txt file.
Command: sort-t:-k3-n/etc/passwd | Tail | Tr ' A-Z ' A-Z ' >/tmp/maxuser.txt


Sort usage
The sort command helps us sort by different data types, and its syntax and common parameter formats
Parameters
-B ignores whitespace characters that begin before each line.
-C checks whether the files are sorted in order.
-f sort, ignores uppercase and lowercase letters.
-M sorts the first 3 letters according to the abbreviation of the month.
-N Sorts by the size of the numbers.
-o< the output file > The sorted result into the specified file.
-R is sorted in reverse order.
-t< Delimited character > specifies the field separator character to use when sorting.
-K selects which interval to sort.

TR usage
TR Conversion characters

5, take out the IP address of the current host, hint: The result of ifconfig command is sliced.
Command: Ifconfig | grep ' [0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3}. [0-9] {1,3} ' |cut-d '-f10 |head-n1


Head usage
Show start to standard output, the default head command prints the first 10 lines of its corresponding file.
Parameters
-Q Hide file names
-V Show file name
-c< bytes > Display bytes
-n< rows > rows displayed

6. List the file names of all files ending with. conf in/etc directory and convert other names to uppercase after saving the/tmp/etc.conf file.
Command: find/etc/-name "*.conf" | Tr ' A-Z ' A-Z ' >/tmp/etc.conf


-name refers to the file name

7. Displays the total number of sub-directories or files in the/var directory.
Command: ls-d/var/* | Wc-l

8. Remove the name of the 10 group with the lowest value in the third field in the/etc/group file
Command: sort-t:-k3-n/etc/group | Cut-d:-f1 | Head

9, the contents of the/etc/fstab and/etc/issue files are merged into the same content and saved to the/tmp/etc/test file.
Command: cat/etc/fstab/etc/issue >/tmp/etc.test

10. Please summarize how to use the user group management commands and complete the exercise:
(1), create group distro, its GID is 2016;
Command: Groupadd distro-g 2016

(2), create user Mandriva, whose ID number is 1005, its group is distro;
Command: Useradd mandriva-u 1005-g distro

(3), create user Mageia, whose ID number is 1100, home directory is/bome/linux
Command: Useradd mageia-u 1100-s/home/linux

(4), to the user Mandriva add password, but keep their home directory
Command: Echo Mageia | passwd--stdin Mageia

(5), delete Mandriva, but retain their home directory;
Command: Userdel Mandriva

(6), create user Slackware, whose ID number is 2002, the basic group is distro, the additional group is Peguin;
Command: Useradd slackware-u 2002-g distro-g Peguin

(7), modify the default shell for Slackware/bin/tcsh
Command: Usermod-s/bin/tcsh Slackware

(8), for the user Slackware New Add additional group admins;
Command: usermod-a-G admins Slackware

(9), add a password for slachware, and require a minimum password age of 3 days, the maximum is 180 days, warning for 3 days.
Command: passwd slackware-n 3-x 180-w 3

(10), add user OpenStack, whose ID number is 3003, the basic group is clouds, the additional group is Penguin and Nova;
Command: Useradd openstack-u 3003-g clouds-g Penguin,nova

(11), add the system user MySQL, request its shell for/sbin/nologin;
Command: Useradd-r mysql-s/sbin/nologin

(12), use the echo command, non-interactive add a password for OpenStack.
Command: Echo OpenStack | passwd--stdin OpenStack

Filter, find, and user and group management commands for Linux files and folders

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.