By Alain Francois
You have probably faced a situation where you are looking for a file, but you don't remember its name; you only remember the contents. It is possible to retrieve the file if you remember specific text within the file. Linux systems offer some commands that can help you to do so.
The following step-by-step guide explains how to run your MySQL server from the Alibaba Cloud console.
Log in to your Alibaba Cloud account and go to Elastic Compute Service (ECS):
Create a new instance. We will choose a Pay-As-You-Go instance:
Choose the image Ubuntu 20.04:
Continue with the configuration of your instance until the end:
The following Linux commands explain how to find files containing specific text.
grep
is a built-in Linux command that prints lines that match a given pattern. It returns all the lines of a file that contain a certain string by default, and the command is also case-sensitive. grep
offers some interesting. You can tell the command to search in subdirectories, ignore the case, and more, using specific parameters.
i
to ignore the caser
for a recursive searchl
to only print the names of the file containing the pattern linesn
shows the line number containing the patternThe syntax is listed below:
grep -ri "pattern" /directory-path
You can try to look for files with the "systemd" pattern:
$ sudo grep -rni "systemd" /etc/
etc/group-:40:systemd-journal:x:101:
/etc/group-:41:systemd-network:x:102:
/etc/group-:42:systemd-resolve:x:103:
/etc/group-:43:systemd-timesync:x:104:
/etc/group-:55:systemd-coredump:x:999:
/etc/default/networkd-dispatcher:2:# by the included systemd service file.
/etc/default/irqbalance:3:# seconds. This is the environment file that is specified to systemd via the
/etc/default/rsync:4:# If this system uses systemd, you can specify options etc. for rsync
/etc/default/rsync:5:# in daemon mode by copying /lib/systemd/system/rsync.service to
/etc/default/rsync:6:# /etc/systemd/system/rsync.service and modifying the copy; add required
/etc/default/chrony:2:# /lib/systemd/system/chrony.service; it allows you to pass various options to
....
....
The line number can help you. Then, you can filter the result to only have the filename with awk
and remove duplicated filename:
$ sudo grep -rni "systemd" /etc/ | awk -F: '{print $1}' | uniq
etc/group-
/etc/default/networkd-dispatcher
/etc/default/irqbalance
/etc/default/rsync
/etc/default/chrony
/etc/cron.daily/apt-compat
/etc/cron.daily/logrotate
/etc/cron.daily/man-db
....
....
It must be easier to filter the filename by using the -l
parameter since it will remove the duplicated file automatically:
$ sudo grep -lri "systemd" /etc/
/etc/group-
/etc/default/networkd-dispatcher
/etc/default/irqbalance
/etc/default/rsync
/etc/default/chrony
/etc/cron.daily/apt-compat
/etc/cron.daily/logrotate
/etc/cron.daily/man-db
/etc/apparmor.d/usr.sbin.rsyslogd
/etc/apparmor.d/abstractions/libpam-systemd
/etc/apparmor.d/abstractions/nameservice
/etc/apparmor.d/abstractions/base
/etc/apparmor.d/abstractions/dbus-session-strict
....
....
If you want to search in the current directory, you only have to omit the path:
$ grep -ri "systemd"
You can use the -w
parameter if you only want to display lines that match the whole word.
You can also use the find
command that you can combine with the grep
command:
find /directory-path -type f -exec grep -l "pattern" {} \;
This command gives you output without a duplicated file:
$ sudo find /etc -type f -exec grep -l "passwd" {} \;
/etc/rpc
/etc/default/nss
/etc/cloud/cloud.cfg
/etc/apparmor.d/abstractions/authentication
/etc/apparmor.d/abstractions/ubuntu-browsers.d/java
/etc/apparmor.d/abstractions/nameservice
/etc/pam.d/chfn
/etc/pam.d/passwd
/etc/pam.d/su
1,076 posts | 263 followers
FollowAlibaba Clouder - October 2, 2018
Alibaba Clouder - January 2, 2019
Alibaba Cloud Community - December 31, 2021
Alibaba Clouder - November 19, 2019
Alibaba Clouder - April 23, 2019
Alibaba Clouder - January 2, 2019
1,076 posts | 263 followers
FollowAlibaba Cloud Linux is a free-to-use, native operating system that provides a stable, reliable, and high-performance environment for your applications.
Learn MoreA Web browser-based admin tool that allows you to use command line tools to manage Alibaba Cloud resources.
Learn MoreRespond to sudden traffic spikes and minimize response time with Server Load Balancer
Learn MoreCloud-based and lightweight servers that are easy to set up and manage
Learn MoreMore Posts by Alibaba Cloud Community